How to add unique keys to Sanity array items without ID field?

15 replies
Last updated: Nov 30, 2025
I have an
Array
currently which i query from graphql, and then I map it into a
<li>
in Gatsby. However, React throws the missing
key
error. I have no unique key within my
Array
. How should I go about working around this? Do I create an object with two key value pairs, one that has an
id
and the other with my
Array
?
AI Update

React requires a unique key prop when rendering lists to efficiently track which items have changed. You have a few options here:

1. Use the array index (quick fix, but not ideal)

{myArray.map((item, index) => (
  <li key={index}>{item}</li>
))}

This works but isn't recommended if your array might be reordered, filtered, or items added/removed, as it can cause rendering issues.

2. Add a _key field in Sanity (recommended) If this array is coming from a Sanity document, you should add a _key field to each array item in your schema. Sanity automatically generates _key values for objects within arrays:

{
  name: 'myArray',
  type: 'array',
  of: [{
    type: 'object',
    fields: [
      {name: 'content', type: 'string'}
      // your other fields
    ]
  }]
}

Then in your GraphQL query, make sure to include _key:

{
  myArray {
    _key
    content
  }
}

And use it in your component:

{myArray.map((item) => (
  <li key={item._key}>{item.content}</li>
))}

3. Generate a stable key from the content If you can't modify the schema and the values are unique, you could use the content itself:

{myArray.map((item) => (
  <li key={item}>{item}</li>
))}

The _key approach is definitely the cleanest solution when working with Sanity, as it's designed exactly for this purpose. Every object in an array automatically gets a _key field that persists across edits, making it perfect for React keys.

Show original thread
15 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?