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

24 replies
Last 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:

  1. Custom block components receive a value prop (not node - that's the old API) containing all the data from your block
  2. Define them in the types object within the components prop
  3. Return JSX directly from your component functions

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

  • The type names in your components.types object match exactly what you defined in your Sanity schema
  • You've included all custom block types that appear in your content

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

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?