How to set the initialValue of a block field in Sanity.io

5 replies
Last updated: Sep 11, 2023
Hello đź‘‹I'm trying to pre-fill or set the
initialValue
of a field of type
block
. I get the following warning on the Studio side. Clicking "Set a new random_key.." seems to work but is there a way of this being automatically set without needing to click it?
        defineField({
          title: "Description",
          name: "description",
          type: "array",
          of: [{ type: "block" }],
          initialValue: [
            {
              _type: "block",
              children: [
                {
                  _type: "span",
                  marks: [],
                  text: "Hello there\n\nThis is a new line!",
                },
              ],
              markDefs: [],
              style: "normal",
            },
          ],
        }),
AI Update

Yes, you're encountering the _key warning because block content in Sanity requires unique _key properties for each object in the array structure, including both the block itself and its children.

The solution is to explicitly add _key values using a UUID generator. Here's the working approach:

import {v4 as uuidv4} from 'uuid'

defineField({
  title: "Description",
  name: "description",
  type: "array",
  of: [{ type: "block" }],
  initialValue: [
    {
      _key: uuidv4(),
      _type: "block",
      children: [
        {
          _key: uuidv4(),
          _type: "span",
          marks: [],
          text: "Hello there\n\nThis is a new line!",
        },
      ],
      markDefs: [],
      style: "normal",
    },
  ],
})

Important notes:

  • You need to add _key to both the block object and each child span object
  • The _key values must be unique identifiers (UUID is perfect for this)
  • Unfortunately, there's no way to auto-generate these keys directly in the initialValue - you must set them manually in your schema definition

There is an autoGenerateArrayKeys: true option in the Sanity client config, but this only applies when writing documents via the client API, not for schema-level initialValue definitions. As confirmed in the Sanity community discussion, it's not possible to use this approach with initialValue.

The manual UUID approach is the recommended solution and will eliminate the warning while ensuring your initial block content is properly structured.

You need to either explicitly add a
_key
property to any object in that array or use the
{autoGenerateArrayKeys: true}
commit config.
Thanks
user M
I'm using
uuid
and this works!
        defineField({
          ...
          type: "array",
          of: [{ type: "block" }],
          initialValue: [
            {
              _key: uuidv4(),
              children: [
                {
                  _key: uuidv4(),
                },
              ],
              markDefs: [],
              style: "normal",
            },
          ],
The second options could be nicer as it won't need me to set myself. The link you shared points to the write API - how would this be set on the schema
initialValue
?
It wouldn’t be possible in an initial value, unfortunately.
I see, thanks for the help anyway
user M
:gratitude-thank-you:
You’re welcome!

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?