retrieve the values of sibling fields to use in rendering an inputComponent

3 replies
Last updated: Feb 7, 2021
How can I retrieve the values of sibling fields to use in rendering an inputComponent? For example I want to render the text value but with the font specified in the fontSize sibling field.
AI Update

You can access sibling field values in a custom input component using the useFormValue hook from Sanity Studio. This lets you read values from other fields in the same document and use them to customize how your component renders.

Here's how to implement your specific example of rendering text with a font size from a sibling field:

import { useFormValue } from 'sanity'

const CustomTextInput = (props) => {
  // Access the sibling fontSize field
  const fontSize = useFormValue(['fontSize'])
  
  return (
    <div>
      <input
        {...props.inputProps}
        style={{ fontSize: fontSize || '16px' }}
        value={props.value || ''}
        onChange={(e) => props.onChange(e.target.value)}
      />
    </div>
  )
}

export default CustomTextInput

How it works

The useFormValue hook accepts a path array that specifies which field to access:

  • Sibling field: useFormValue(['fieldName'])
  • Nested field: useFormValue(['parent', 'child', 'fieldName'])
  • Entire document: useFormValue([])

The hook automatically re-renders your component when the referenced field changes, so your text input will dynamically update its styling as the user modifies the fontSize field.

Common patterns

Multiple sibling fields:

const fontSize = useFormValue(['fontSize'])
const fontWeight = useFormValue(['fontWeight'])
const color = useFormValue(['color'])

// Apply all styles
<input style={{ fontSize, fontWeight, color }} />

Conditional rendering based on sibling:

const showAdvanced = useFormValue(['showAdvancedOptions'])

return showAdvanced ? <AdvancedInput {...props} /> : <SimpleInput {...props} />

Working with array items: When your input is inside an array, you can access other fields in the same array item using the path:

const { path } = props
const parentPath = path.slice(0, -1)
const siblingValue = useFormValue([...parentPath, 'siblingFieldName'])

Important notes

  • useFormValue only works inside custom input components within the form builder context—you can't use it in document actions or other Studio contexts
  • This hook replaced the old v2 pattern of accessing props.document or props.parent
  • The hook returns undefined if the field doesn't exist or hasn't been set yet, so provide fallback values

This approach gives you full control over creating dynamic, interdependent form experiences without needing to make API calls or manage complex state yourself.

One thing I tried to do was to do a client.fetch query but the problem here is that I could find any kind of id being passed in a a prop in my “TextInput” component, so it wouldn’t be possible to corroborate this data to the data queried from the api.
This is a view of the input component as of now. I cropped the picture below where I put in my projectid and api key for obvious reasons.
Hi Matthew, could you try wrapping your component in
withDocument()
? As follows:
import { withDocument } from 'part:@sanity/form-builder'

...

const TextInput = (props) => {
 ...
}

export default withDocument(TextInput)
This should give you access to a
document
prop that contains your document's content.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?