How to access parent object in custom input component in Sanity v3?

13 replies
Last updated: Jan 2, 2023
Hello! It seems that the
parent
object is no longer sent to custom input component in sanity v3. How would I go about getting context about the current object in the new version? The docs i can find (https://www.sanity.io/guides/your-first-input-component-for-sanity-studio-v3 ) doesn't mention anything about it. In v2 i could access it via
props.parent
AI Update

In Sanity Studio v3, the parent object is no longer automatically passed as a prop to custom input components. Instead, you need to use the useFormValue hook to access parent or document values.

Accessing Parent Values in v3

Here's how to migrate from the v2 approach to v3:

In v2, you used props.parent:

import {withDocument} from 'part:@sanity/form-builder'

function MyInput(props) {
  const parentValue = props.parent.someField
  return <div>{/* ... */}</div>
}

export default withDocument(MyInput)

In v3, use the useFormValue hook:

import {useFormValue} from 'sanity'

function MyInput(props) {
  // Access a specific field from the parent/document
  const parentValue = useFormValue(['someField'])
  
  // Or access the entire document
  const document = useFormValue([])
  
  return <div>{/* ... */}</div>
}

export default MyInput

How useFormValue Works

The useFormValue hook takes an array representing the path to the value you want to access:

  • useFormValue([]) - Gets the entire document
  • useFormValue(['title']) - Gets the title field
  • useFormValue(['author', 'name']) - Gets nested values

For accessing parent object values when your input is nested inside an object, you can use props.path to construct the parent path:

import {useFormValue} from 'sanity'

function MyInput(props) {
  // Get parent path by removing the last segment
  const parentPath = props.path.slice(0, -1)
  const parentValue = useFormValue(parentPath)
  
  return <div>{/* ... */}</div>
}

This change was part of the Studio v2 to v3 migration, where the parts system was deprecated in favor of React hooks for better composability and type safety.

Show original thread
13 replies
Hi thanks for your message. I havnt accessed the link you shared yet. Do you think i should continue installing v3 rather 2?
const docId = String(useFormValue(["_id"]));
Hi, I did not find any documentation on this yet, but I’m using
useFormValue
. For example getting the _id of the document could be done like this:
import { useFormValue } from "sanity";
const docId = String(useFormValue(["_id"])); 
Thanks
user U
! That did the trick. I wanted to access the parent in a nested object inside the document, but I was able to get that from using
useFormValue(props.path.slice(0, -1))
(slice to remove the last element from the array, as without it I would just get the custom components value)
user H
With this fixed, I was able to migrate all my things to v3 relatively easily, even with a couple of custom plugins and components. I will say the docs are still somewhat lacking, but it's much much better than when it was in beta 🙂
Thanks Ill give it go.
Ok as i mentioned. I am pretty new to this so the document means nothing to me. What i mean is where is that document? is it something that has been written by yourself? Sorry to sound bit thick. However thanks all the same so far.
user H
no worries, be nice to yourself: nobody sounds thick and there are no stupid questions :sanity_with_3_hearts:I am a bit unsure, which document you mean.
As you know, you have to kinds of types: document and objects. Where are you stuck in the migration?
Hi Saskia, At present i'm not to sure where to look or to resolve the error i got
[plugin:vite:css] [postcss] Cannot read properties of undefined (reading 'config')
    at getTailwindConfig (/Users/neillwhite/builds/portfolio-nw/node_modules/tailwindcss/lib/lib/setupTrackingContext.js:84:63)
    at /Users/neillwhite/builds/portfolio-nw/node_modules/tailwindcss/lib/lib/setupTrackingContext.js:96:92
    at /Users/neillwhite/builds/portfolio-nw/node_modules/tailwindcss/lib/processTailwindFeatures.js:46:11
    at plugins (/Users/neillwhite/builds/portfolio-nw/node_modules/tailwindcss/lib/index.js:38:63)
    at LazyResult.runOnRoot (/Users/neillwhite/builds/portfolio-nw/sanity/node_modules/postcss/lib/lazy-result.js:339:16)
    at LazyResult.runAsync (/Users/neillwhite/builds/portfolio-nw/sanity/node_modules/postcss/lib/lazy-result.js:393:26)
    at LazyResult.async (/Users/neillwhite/builds/portfolio-nw/sanity/node_modules/postcss/lib/lazy-result.js:221:30)
    at LazyResult.then (/Users/neillwhite/builds/portfolio-nw/sanity/node_modules/postcss/lib/lazy-result.js:206:17)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5

This is another matter, did you post this in help already?
Oh yes I did already answer in there. Please dont crosspost, because it makes our work harder 😉 https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1672668729388019
oh yes i did apologies. Ill head over to your link. Hopefully i can get somewhere with that

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?