How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

I’m trying to insert an item into an array in a custom input component but nothing happens.

5 repliesLast updated: Nov 29, 2025

Can anyone help me here? I’m trying to insert an item into an array in a custom input component but nothing happens. I’m importing PatchEvent like this,
import PatchEvent from 'part:@sanity/form-builder/patch-event'
.And then supposedly firing in an
onAction
handler here;
const handleAction = (field) => {
    const patches = [
      { path: [], type: "setIfMissing", value: [] }, 
      {
        type: "insert",
        items: [{ _type: field.name }],
        path: [-1]
      }
    ]

    PatchEvent.from(patches)
}
But the value is never updated.
AI Update

I can see the issue! You're creating the PatchEvent but not actually calling onChange with it. PatchEvent.from() just creates the action - you need to pass it to the onChange callback that's provided to your custom input component.

Here's what your code should look like:

const handleAction = (field) => {
  const patches = insert(
    [{ _type: field.name }],
    'after',
    [-1]
  )
  const patchEvent = PatchEvent.from(patches).prepend(setIfMissing([]))
  
  onChange(patchEvent) // This is the crucial part!
}

Key points:

import PatchEvent, { set, unset, setIfMissing, insert } from 'part:@sanity/form-builder/patch-event'

Note on Studio versions: The import you're using (part:@sanity/form-builder/patch-event) is for Sanity Studio v2. If you're on Studio v3, the import path has changed to sanity and the custom input component API is different - you'd use the set() and insert() functions from the sanity package directly.

The pattern shown above follows the documented approach for array insertions in custom components, ensuring your changes properly sync with the Content Lake and work with real-time collaboration.

Show original thread
5 replies

Was this answer helpful?

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.

Related contributions