Accessing document data in custom input components after v2 to v3 migration

4 replies
Last updated: Mar 7, 2023
Hi there, a question for those who migrated from v2 to v3:

QuestionDo you know how to get access to the document in custom input components? Or pass specific parent information to a custom input component?


ContextWe are using information from the document in our custom inputs quite a lot. And it seems to me that the document isn’t part of the props anymore that are passed to a custom input component.

I’ve used this docs page btw:
https://www.sanity.io/docs/migrating-custom-input-components
AI Update

Great question! Yes, in Sanity Studio v3, accessing document data in custom input components works differently than in v2. The document is no longer passed directly as props, but you can access it using the useFormValue hook.

Quick Solution

To access the entire document in your custom input component:

import { useFormValue } from 'sanity'

const MyCustomInput = (props) => {
  const document = useFormValue([]) // Empty array returns entire document
  
  // Now you can access any field from the document
  console.log(document._id, document.title, etc.)
  
  return (
    // your component JSX
  )
}

Accessing Specific Fields

You can also access specific fields or parent information by passing a path array:

// Access a specific field
const title = useFormValue(['title'])

// Access nested fields
const authorName = useFormValue(['author', 'name'])

// Access document ID
const docId = useFormValue(['_id'])

// Access parent field (if your input is nested)
const { path } = props
const parentPath = path.slice(0, -1)
const parentValue = useFormValue(parentPath)

Common Pattern: Dynamic Field Dependencies

Here's a practical example where one field depends on another:

import { useFormValue } from 'sanity'

const DynamicListInput = (props) => {
  const categoryId = useFormValue(['category']) // Access sibling field
  const [options, setOptions] = useState([])
  
  useEffect(() => {
    if (categoryId) {
      // Fetch options based on category
      fetchOptions(categoryId).then(setOptions)
    }
  }, [categoryId])
  
  return props.renderDefault({
    ...props,
    schemaType: { 
      ...props.schemaType, 
      options: { list: options } 
    }
  })
}

Important Notes

  • The useFormValue hook only works within the form builder context (i.e., in custom input components). You can't use it in document actions or other contexts outside the form.
  • The hook reactively updates when field values change, so your component will re-render when dependencies change.
  • This is the v3 replacement for the old props.document and props.parent patterns from v2.

The migration guide you mentioned should cover this, but the key takeaway is: use useFormValue instead of accessing document data through props.

Show original thread
4 replies
You can use the useFormValue() hook to grab values from your document.
Thanks User! Let me try that
So good
user M
, it works! 🎉
Awesome!

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?