How to Create an Array of Blocks

1 replies
Last updated: Jun 27, 2022
Hi folks!Can I create an array of blocks?
Jun 27, 2022, 10:45 AM
The
block
type already has to be within an array, and you can’t have an array of arrays in Sanity. Instead you could wrap the
block
in an object and put this inside of an array, as follows:
blockObject.js

export default {
    title: "Block",
    name: "blockObject",
    type: "object",
    fields: [
        {
            title: "Block Text",
            name: "blockText",
            type: "array",
            of: [{ type: "block" }],
        },
    ],
};
exampleDocument.js

export default {
    name: "Example Document",
    title: "exampleDocument",
    type: "document",
    fields: [
        {
            title: "Block Array",
            name: "blockArray",
            type: "array",
            of: [{ type: "blockObject" }],
        },
    ],
};
Jun 27, 2022, 11:06 AM

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?