coreyward/react-portable-text

Empty Array of Blocks returning 'TypeError: Cannot read property 'map' of null'

emmalevesque opened this issue · 1 comments

I have a schema that includes PortableText that is filled in some entries and empty in others. I'm using React-Portable-Text to determine if the array is filled — for some reason simply evaluating the array by counting it's length always threw errors. This ended up working for me in most cases but I've just started noticing edge cases where it throws TypeError: Cannot read property 'map' of null

Anyone have an idea of why this is happening? Can you reproduce?

The content prop is required. If you have a field with no data, you're probably passing either null or undefined as the value. I usually do something like the following for optional fields like this:

<div>
  <h1>{title}</h1>
  {description && <PortableText content={description} />}
</div>