Help with block content to React errors

43 replies
Last updated: Oct 4, 2021
Hello! New to the Slack, been using and really enjoying Sanity for a bit now. I'm having some trouble with block content to React on a new project -- is anyone around who could chat about some errors I'm getting?
AI Update

Welcome to the community! Happy to help you with your Portable Text issues.

The main thing to know is that Sanity has moved from the older @sanity/block-content-to-react package to the newer @portabletext/react library for rendering block content in React. If you're getting errors, this is often the first thing to check - make sure you're using the right package.

Here's the basic setup with @portabletext/react:

npm install @portabletext/react

Then in your React component:

import { PortableText } from '@portabletext/react'

function MyComponent({ content }) {
  return <PortableText value={content} />
}

Common issues and fixes:

  1. Wrong package: If you're using @sanity/block-content-to-react, switch to @portabletext/react
  2. Custom blocks/marks: If you have custom content types (images, custom components, etc.), you'll need to pass custom serializers via the components prop
  3. Missing data: Make sure your GROQ query is actually fetching the block content field

For custom components, the pattern looks like:

<PortableText 
  value={content}
  components={{
    types: {
      image: ({value}) => <img src={value.imageUrl} />
    },
    marks: {
      link: ({children, value}) => <a href={value.href}>{children}</a>
    }
  }}
/>

If you can share the specific error message you're seeing, that would help narrow down the issue! Common error messages include things like "Cannot read property 'map' of undefined" (usually a data fetching issue) or complaints about missing serializers (means you have custom content types that need components defined).

The Portable Text introduction guide and beginner's guide to Portable Text are great resources for understanding how it all works under the hood. Portable Text is Sanity's JSON-based rich text format that gives you platform-agnostic content you can render anywhere - once you get past the initial setup, it's really powerful for reusing content across different platforms!

Show original thread
43 replies
What errors are you getting?
Hi
user Y
! I'm using
@sanity/block-content-to-react
and adding a serializer for the
citation
type that I've added in, but I keep getting
Unknown block type "undefined", please specify a serializer for it in the `serializers.types` prop
I tried two different ways of implementing the custom block within my schema and got the same error both times. Happy to share how I structured that if it's helpful.
This suggests that there’s something up with the
blocks
data I think
Is it the whole Portable Text array? Do you see anything funky with the data if you console.log it?
Is it the whole Portable Text array? Do you see anything funky with the data if you console.log it?
That's part of what's interesting, the date looks pretty normal in the console log, but I don't see any
type
attached to any of the children. Could that be part of the problem?
The
_type: 'citation'
 should be on the root objects. So there might be something going on here. Are you using framework or anything?
If you have code to look at, that’s helpful!
I do! One sec, I'll push up the latest code.
I do! One sec, I'll push up the latest code.
cool - where in the project do I find the serializer?
cool - where in the project do I find the serializer?
hmm… weird. looks alright to me
is the dataset private?
Currently yes unfortunately. Would looking at the schema help? I built it here: https://github.com/throneless-tech/recoding-tech/blob/design-implementation/studio/schemas/portable_text.js
ah, this is an annotation
ah, this is an annotation
try to put it under
marks
 instead of
types
 in the serializer
Yes! Is the structure incorrect?
ah, this is an annotation
Ah gotcha, thanks so much. I'll give that a try
I suspect it’s this pattern that you’re trying at https://www.sanity.io/guides/portable-text-internal-and-external-links
Ah yes that's exactly what I was working from
So progress, I'm getting a new error 😆
TypeError: Cannot read property 'find' of undefined
It feels like you're not getting data into the component
It's definitely reading other fields that I've added, like the title and even other references. And if I log the object, all the data appears to be there
It's definitely reading other fields that I've added, like the title and even other references. And if I log the object, all the data appears to be there
Now getting the error
TypeError: can't access property "find", markDefs is undefined
Now getting the error
TypeError: can't access property "find", markDefs is undefined
I need to run offline for a bit but will be back and would love to hear if you have any other ideas. Thanks so much for your help this far!
I need to run offline for a bit but will be back and would love to hear if you have any other ideas. Thanks so much for your help this far!
Now getting the error
TypeError: can't access property "find", markDefs is undefined
It would help to access the data. I’m not super familiar with Stackbit, and it’s not super clear to me how they handle the data fetching
Haha ok totally fair, I'm not sure either. Makes sense that it could be something with stackbit, and I'll see if I can make the dataset accessible!
Haha ok totally fair, I'm not sure either. Makes sense that it could be something with stackbit, and I'll see if I can make the dataset accessible!
Hi
user Y
! Following up on this because I think I figured out the problem and am submitting a PR to stackbit. My new question though is how best to format a serializer for an annotation. Is there a recommended format for adding a mark that's a reference? I see the example usage for a mark that's a highlighter, but what if the mark is a reference to another record?
Hi
user Y
! Following up on this because I think I figured out the problem and am submitting a PR to stackbit. My new question though is how best to format a serializer for an annotation. Is there a recommended format for adding a mark that's a reference? I see the example usage for a mark that's a highlighter, but what if the mark is a reference to another record?

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?