How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

How to define custom types as JSX elements in PortableText components?

24 repliesLast updated: Nov 29, 2025

Hello!I am trying to se PortableText for this query:
I don't know how can I define the
hero
and
content
types in my components options.To be more specific, I want to define
hero
and
content
as JSX elements
AI Update

Based on your question, it sounds like you have custom block types called hero and content in your Portable Text schema, and you want to define how they render as JSX elements in your React components.

Here's how you can define custom block types as JSX elements using the @portabletext/react library:

import { PortableText } from '@portabletext/react'

// Define your custom components
const HeroComponent = ({ value }) => {
  return (
    <div className="hero">
      <h1>{value.title}</h1>
      <p>{value.description}</p>
      {/* Access other fields from your hero block */}
    </div>
  )
}

const ContentComponent = ({ value }) => {
  return (
    <section className="content-section">
      {/* Render whatever fields your content block has */}
      <div>{value.body}</div>
    </section>
  )
}

// Define the components object
const ptComponents = {
  types: {
    hero: HeroComponent,
    content: ContentComponent,
    // Add other custom block types here
  }
}

// Use it in your render
function MyPage({ portableTextData }) {
  return (
    <PortableText 
      value={portableTextData}
      components={ptComponents}
    />
  )
}

The key points from the Portable Text documentation:

If you're getting an "Unknown block type" error, make sure:

You can also check out this comprehensive guide on customizing Portable Text which covers the full flow from schema definition to React rendering.

Show original thread
24 replies

Was this answer helpful?

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.

Related contributions