Help with block content to React errors
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/reactThen in your React component:
import { PortableText } from '@portabletext/react'
function MyComponent({ content }) {
return <PortableText value={content} />
}Common issues and fixes:
- Wrong package: If you're using
@sanity/block-content-to-react, switch to@portabletext/react - Custom blocks/marks: If you have custom content types (images, custom components, etc.), you'll need to pass custom serializers via the
componentsprop - 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 thread43 replies
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.