Using `useFormValue` to access current document properties in Sanity

4 replies
Last updated: Jun 25, 2024
We have a field that is using a custom input component `AsyncListInput`:

defineField({
    name: "crmUpdateFieldId",
    type: "string",
    options: { /* snipped */ },
    components: {
        input: AsyncListInput,
    },
}),

const AsyncListInput = (props: any) => {
    const { schemaType, renderDefault } = props

    const dossierTypeId = /* Get dossierTypeId from the current document somehow */

    // get values from API 
    const values = /* snipped ... */

    return renderDefault({
        ...props,
        schemaType: { ...schemaType, options: { list: values } },
    })
}
Within
AsyncListInput
we call an API and would like to supply a property value that was set in the document that is currently being edited.Is there any way to access properties from the current document from
AsyncListInput
?I know we can call the Sanity API, get the draft document and take the property value that way but I'm hoping the document is already accessible in another way without doing the API call.
Jun 25, 2024, 8:44 AM
To check that I’m understanding, you want values from a field in the same document that this component is in?
Jun 25, 2024, 3:43 PM
Yes
Jun 25, 2024, 6:08 PM
You can use the
useFormValue
hook to get it then. You need to import that from the
sanity
package and pass it the path to the field.
Jun 25, 2024, 6:12 PM
Brilliant, thanks!!!
Jun 25, 2024, 7:23 PM

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?