Render Sanity Portable Text in React With @portabletext/react
Serialize Sanity Portable Text into React components using @portabletext/react, with a Next.js example and verification steps.
26 Sept 2025, 09:34 UTC

The problem: Sanity rich text is structured JSON, not HTML
When you query a Sanity rich-text field with GROQ, the response is not an HTML string. It is Portable Text: a JSON array of block objects. Each block contains child spans, and spans carry marks such as links or highlights. If you pass that array directly to React or inject it with dangerouslySetInnerHTML, nothing renders as expected. Your design-system heading styles, custom link component, and embedded callouts stay invisible.
Portable Text exists so Sanity Studio can edit rich text collaboratively as structured data. The frontend must serialize that JSON into React components before users can see it.
Use @portabletext/react, not the legacy block-content package
The older @sanity/block-content-to-react package is deprecated. New frontend work should use @portabletext/react, the maintained successor. It renders Portable Text blocks, spans, and marks and lets you supply serializers that map Sanity styles and annotations to your own components.
Install it in your project root. No special permissions are required beyond normal package-install access.
npm install @portabletext/react @sanity/client
Worked example: Next.js component with custom serializers
1. Query the full Portable Text structure
Request markDefs alongside blocks and spans. Without mark definitions, link marks cannot resolve to their URLs.
*[_type == "post"]{
title,
body[]{
_key,
_type,
style,
listItem,
children[]{
_key,
_type,
text,
marks
},
markDefs[]{
_key,
_type,
href
}
}
}
2. Map blocks and marks to components
Create serializers for the styles and marks your editors use. This example maps an h2 block to a heading component and a link mark to a local Link component. Serializer signatures vary between major versions of @portabletext/react; check the documentation for the version installed in your project before adapting this code.
import { PortableText } from '@portabletext/react';
import Link from '@/components/Link';
const serializers = {
blocks: {
h2: ({ children }) => {children}
,
},
marks: {
link: ({ markDef, children }) => (
{children}
),
},
};
export default function SanityBody({ body }) {
if (!body) return null;
return ;
}
Run this in a Next.js page or component that receives Sanity data through your existing client. The value prop expects the raw Portable Text array from GROQ.
Expected check
Open the rendered page, right-click a linked word inside the rich text, and inspect the element. It should render through your Link component, typically as an anchor tag with your component's class names. If the link appears as plain text, the link mark serializer is missing or markDefs were not included in the GROQ projection.
Risk: unregistered custom objects disappear silently
If editors embed a custom object type, such as a product card, inside Portable Text and you do not register a serializer for that type, the block may render as an empty container or be skipped. The page still loads, but the embedded content is visually absent. Add a types serializer for each custom object your schema allows.
Limitations and practical verification
Portable Text payloads are larger than raw HTML because every span and mark is a JSON object. Deeply nested blocks and embedded custom objects increase client rendering cost and can contribute to layout shifts when block dimensions vary.
Never render Portable Text through dangerouslySetInnerHTML. It is not HTML, and custom object data can contain executable markup if trusted blindly.
Practical check: render a sample Portable Text document locally, then compare the GROQ projection output against the serialized DOM. Confirm no blocks or spans are missing, especially inside nested lists or custom objects.
Closing
Treat Portable Text as structured data, not HTML. Use @portabletext/react with serializers that match your design system, include markDefs in your GROQ projection, and verify the serialized output against the raw JSON before deploying.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.