Unlock seamless workflows and faster delivery with our latest releases- get the details

Question about creating an array of PortableText blocks in Sanity

2 replies
Last updated: Dec 8, 2022
Quick question regarding PortableText. I want to have a field that is an array of portable text. For a single portable text, I would create an array of block
type: 'array',
of: [{type: 'block'}]
But if I want this field to be a repeater to allow admin to create multiple different portableText blocks, what I tried is create a component that is an array of block and then creating a field that is an array of that component. But I seem to get an error when doing that.


// PortableText component
export default defineType({
  name: 'portableText',
  title: 'Portable Text',
  type: 'array',
  of: [{
    type: 'block',
    styles: [...],
    marks: {...}
  }]
})

// Inside my other document
...
{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'array',
  of: [{type: 'portableText'}],
  group: 'content',
}
This gives me the following error when opening the page

Error: Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')
    at ListArrayInput (<http://localhost:5173/node_modules/.vite/deps/chunk-RZPXVCEY.js?v=35974456:87010:364>)
    at renderWithHooks (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:16007:26>)
    at mountIndeterminateComponent (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:18736:21>)
    at beginWork (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:19691:22>)
    at beginWork$1 (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:23492:22>)
    at performUnitOfWork (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22940:20>)
    at workLoopSync (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22876:13>)
    at renderRootSync (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22855:15>)
    at recoverFromConcurrentError (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22477:28>)
    at performSyncWorkOnRoot (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22619:28>)
Dec 8, 2022, 2:05 PM
The Studio doesn’t support arrays-directly-in-arrays, so you need to wrap it in an object:
{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'object',
  fields: [{
   type: 'portableText'
   name: 'content', // grow_notes.content
  }],
  group: 'content',
}

Dec 8, 2022, 2:08 PM
Thanks!
Dec 8, 2022, 3:19 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?