Accessing document data in custom input components after v2 to v3 migration
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
useFormValuehook 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.documentandprops.parentpatterns 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 thread4 replies
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.