How to update Sanity custom input field value with PatchEvent?

5 replies
Last updated: Nov 30, 2025
Im trying to make a custom input component that takes in a name from the input text field, calls the poke api, then i’m trying to then output the id from that data and then create an image preview of the pokemon from a a url based upon what the ID that is returned is in what I think will be an avatar from the sanity ui (haven’t even got to that bit given I cant get over my first hurdle!!). I created it in the input component as an object using User’s alpha cli , but i’m not entirely sure how I update the values correctly because I don’t fully understand the field patch event. Im not great at explaining so i’ve included some code (and Im sorry if Ive gone about how I do this completely wrong in the first place, Im learning so please forgive me!)
getPokeData(value.pokemonName);

  async function getPokeData(pokemonName) {
    const pokeApi = `<https://pokeapi.co/api/v2/pokemon/${pokemonName}>`;
    const res = await fetch(pokeApi);
    const data = await res.json();
    const pokeId = data.id;
      
  }
above is my code for getting the data, please can someone help explain how I would set the data.id into my field I have created for the ID.
AI Update

Great question! I totally understand the confusion around PatchEvent - it's one of those things that clicks once you see it in action. Let me help you update your Pokemon ID field properly.

The key thing to understand is that in Sanity v3, you don't actually need to import PatchEvent anymore. You can import the patch operations (set, unset, setIfMissing) directly from 'sanity' and pass them to your onChange callback.

Here's how to update your code:

import { set, setIfMissing } from 'sanity';

// In your custom input component
export function PokemonInput(props) {
  const { onChange, value = {} } = props;

  async function getPokeData(pokemonName) {
    const pokeApi = `https://pokeapi.co/api/v2/pokemon/${pokemonName}`;
    const res = await fetch(pokeApi);
    const data = await res.json();
    const pokeId = data.id;
    
    // Update the ID field in your object
    onChange(set(pokeId, ['pokeId'])); // The second argument is the path
  }

  return (
    <div>
      <input
        type="text"
        value={value.pokemonName || ''}
        onChange={(e) => {
          const newName = e.target.value;
          // Update the pokemonName field
          onChange(set(newName, ['pokemonName']));
          // Fetch the Pokemon data when name changes
          if (newName) {
            getPokeData(newName);
          }
        }}
      />
      {value.pokeId && <p>Pokemon ID: {value.pokeId}</p>}
    </div>
  );
}

Key points:

  1. The onChange prop is passed to your component and is what actually updates the document
  2. The set() operation takes two arguments: the value to set, and the path (as an array) to where it should be set
  3. For object fields, use the field name as the path: set(value, ['fieldName'])

If you want to update multiple fields at once (like both pokemonName and pokeId), you can do it in one call by setting the entire object:

onChange(set({
  pokemonName: newName,
  pokeId: pokeId
}));

For your schema, you'd want something like:

{
  name: 'pokemon',
  type: 'object',
  components: {
    input: PokemonInput
  },
  fields: [
    { name: 'pokemonName', type: 'string' },
    { name: 'pokeId', type: 'number' },
    { name: 'imageUrl', type: 'string' }
  ]
}

Once you have the ID working, you can build your image URL (https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokeId}.png) and use the Avatar component from @sanity/ui to display it!

The modern v3 approach is much simpler than the old PatchEvent pattern - just import set, unset, and setIfMissing directly from 'sanity' and pass them to onChange. The onChange callback handles all the real-time synchronization and document updates for you. You can read more about how patches work under the hood if you're curious!

Show original thread
5 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.

Was this answer helpful?