Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

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 remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?