Custom component PatchEvent.from(set(value)) not working
Based on the documentation, the most common reason PatchEvent.from(set(value)) appears to do nothing is that you're not actually calling the onChange callback with the PatchEvent.
PatchEvent.from() is just an action creator - it creates the patch event but doesn't trigger any updates by itself. You need to pass it to the onChange prop that Sanity provides to your custom component:
// ❌ This won't work - just creates the event but doesn't dispatch it
PatchEvent.from(set(value));
// ✅ This will work - passes the event to onChange
onChange(PatchEvent.from(set(value)));Here's a complete example of a working custom input component:
import { set, unset } from 'sanity'
export function MyCustomInput(props) {
const { onChange, value } = props
const handleChange = (event) => {
const inputValue = event.target.value
// Create and dispatch the patch event
onChange(
inputValue
? PatchEvent.from(set(inputValue))
: PatchEvent.from(unset())
)
}
return (
<input
type="text"
value={value || ''}
onChange={handleChange}
/>
)
}Key points:
- The
onChangeprop is passed to your component by Sanity's form system - You must call
onChange()with your PatchEvent to actually trigger the update - For Sanity v3, import patch operations from
'sanity'directly:import { set, unset, setIfMissing } from 'sanity'
If you're still not seeing updates after calling onChange, double-check:
- That your component is receiving the
onChangeprop correctly - That the field name in your schema matches where you expect the data to be stored
- For object fields, ensure your keys match the schema field names
You can find more details in the PatchEvent documentation and this community answer about the same issue.
Show original thread4 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.