Custom component PatchEvent.from(set(value)) not working

4 replies
Last updated: Nov 30, 2020
I am trying to set a value with a custom component, but I am getting no response when the
PatchEvent.from(set(value));
executes.
AI Update

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:

  1. The onChange prop is passed to your component by Sanity's form system
  2. You must call onChange() with your PatchEvent to actually trigger the update
  3. 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 onChange prop 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 thread
4 replies
The docs mentions this syntax:
{
  type: 'set' | 'unset' | 'setIfMissing',
  path?: Array<string>,
  value?: any
}

But im not seeing it used in any examples
Hi, sorry for the sparse documentation - we're working on improving this. Do you call your custom input component's
onChange
prop with it anywhere? PatchEvent.from(...) is just a action creator and doesn't do anything on it's own.
There's an example in the render() function here:
https://www.sanity.io/docs/custom-input-widgets#example-implement-a-custom-slider-375a2e4dc198

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?