Using withDocument HOC to refer to the document in a preview component
The short answer is no, you cannot directly access the parent document object in the prepare function of a child object's preview configuration. The document variable in your prepare function is undefined because the standard preview select and prepare functions only have access to the fields you explicitly select from the current object.
However, there's a modern solution for Studio v3: use a custom preview component with the useFormValue hook.
Solution: Custom Preview Component (Studio v3)
Instead of using the standard preview.prepare function, create a custom preview component that can access the parent document's fields:
import React from 'react';
import { useFormValue } from 'sanity';
const ChildObjectPreview = (props) => {
// Access the parent document's showCorrectness field
const showCorrectness = useFormValue(['showCorrectness']);
const { isCorrect, text } = props;
const PARENT_ICON = {
correct: '✅',
incorrect: '❌',
neutral: '🔵',
};
let parentIcon = PARENT_ICON.neutral;
if (showCorrectness) {
parentIcon = isCorrect ? PARENT_ICON.correct : PARENT_ICON.incorrect;
}
return props.renderDefault({
...props,
title: text,
media: <p>{parentIcon}</p>,
});
};
export default {
name: 'childObject',
title: 'Child Object',
type: 'object',
fields: [
// ... your fields
],
components: {
preview: ChildObjectPreview
}
};The useFormValue hook allows you to access any field in the parent document by providing the field path as an array. For accessing nested parent fields, you can use useFormValue(props.path.slice(0, -1)) to get the immediate parent object.
Why the Standard Preview Doesn't Work
The standard preview configuration with select and prepare is designed to work only with the current object's fields and referenced documents. It doesn't have access to the parent document context by design—this keeps previews performant and predictable.
The custom preview component approach is the recommended solution when you need access to parent document fields or other form state in Studio v3.
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.