How to create a unique key for a static list in Gatsby using React and GraphQL

15 replies
Last updated: Jun 17, 2021
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.

user R
normally in react, if you don’t have an ID you can just use the
index
to do something like this:

return (
  <ul>
    {items.map((item, index) => (
      <li key={`item_${index}`}>{item}</li>
    ))}
  </ul>
)
NOTE - this is not a good solution if you expect your list of items to change dynamically at runtime
great thanks! It's a static list and will only change if i rebuild the site.
user R
cool - you won’t have any issues using that method then 👍
great thanks! It's a static list and will only change if i rebuild the site.
as the internet will tell you, using index as keys in react is usually not the best idea
The only thing the key actually needs to do is to be unique, and sanity usually provides a unique key or id field for all the array points, I tend to use them as my keys
That's whats been bugging me. I dont seem to have a unique key or id field for my array and I dont know how to create it at the moment either
and here's my schema

    {
      name: 'info',
      title: 'Gist of product',
      description: 'Allows for a gist to be displayed on main product page',
      type: 'array',
      of: [
        {
          type: 'string',
        },
      ],
    },
There are some properties with an underscore prefixed such as
_id _key
on the left pane of your Graphi interface. These should be what you're looking for 👌
user V
wouldnt these be unique to the
Document
itself? What happens when I have another array within my
Document
?
I assume
_id
will be for each item of the array
ill give it a shot. i assumed the
_id
was for the document
thanks
ill give it a shot. i assumed the
_id
was for the document
I could be wrong 😄 give it a go anyway
I thought you wanted an unique id for each
info
item. But looks like you want a unique id for each string element inside
info
. If you know each string in the array will be unique in the list you could generate an id from that instead.

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?