useFormValue hook missing context in custom document action
The error you're seeing is expected! useFormValue only works within custom input components that are part of the form builder context. It cannot be used in document actions because actions don't have access to the form context.
According to the Sanity documentation on useFormValue, attempting to use it outside of custom input components will result in the "FormBuilder: missing context value" error you're encountering.
For document actions, you should use props.draft and props.published to access document values instead:
export function SetAndPublishAction(props: any) {
const { patch, publish } = useDocumentOperation(props.id, props.type)
const [isPublishing, setIsPublishing] = useState(false)
const dataset = useDataset()
// Access document data from props instead of useFormValue
const country = props.draft?.country || props.published?.country
useEffect(() => {
if (isPublishing && !props.draft) {
setIsPublishing(false)
}
}, [props.draft])
return {
disabled: publish.disabled,
label: isPublishing ? 'Publishing' : 'Publish & Update',
onHandle: () => {
setIsPublishing(true)
// You can now use the country value here
console.log("Country value:", country)
patch.execute([{ set: { name: "hello world" } }])
console.log("Dataset:", dataset)
publish.execute()
props.onComplete()
},
}
}Key points:
props.draftcontains the current draft document (if it exists)props.publishedcontains the published version- For the most current data, check
props.draftfirst, then fall back toprops.published - You can access any field using standard JavaScript object notation:
props.draft?.country
This is the correct pattern for accessing document values in custom document actions!
Show original thread5 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.