Using block-content-to-react with Gatsby GraphQL data

13 replies
Last updated: Nov 29, 2025
Are there any videos on how to use the block-content-to-react (https://github.com/sanity-io/block-content-to-react/blob/master/README.md ) inside a gatsby app where I’m already getting the data via gatsby graphql api already?
AI Update

I searched for specific video tutorials on using block-content-to-react with Gatsby, but unfortunately couldn't find dedicated video content on this exact topic. While "Learn With Jason" has done Sanity episodes, I wasn't able to locate a specific Gatsby + Portable Text video tutorial.

However, since you already have your data coming through Gatsby's GraphQL API, I can help guide you through the implementation!

Important Note on block-content-to-react

The block-content-to-react package you linked is now considered legacy. The modern, recommended approach is to use @portabletext/react instead, which offers better performance and more flexibility.

Using Portable Text with Gatsby

Since you're already getting data via Gatsby's GraphQL API, here's how to render it:

npm install @portabletext/react

Then in your Gatsby component:

import { PortableText } from '@portabletext/react'

function BlogPost({ data }) {
  return (
    <article>
      <h1>{data.sanityPost.title}</h1>
      <PortableText value={data.sanityPost._rawBody} />
    </article>
  )
}

export const query = graphql`
  query($slug: String!) {
    sanityPost(slug: { current: { eq: $slug } }) {
      title
      _rawBody
    }
  }
`

Key point: Use _rawBody (or whatever your field name is with the _raw prefix) in your GraphQL query to get the raw Portable Text data structure that the renderer expects.

If you still want to use the legacy block-content-to-react:

npm install @sanity/block-content-to-react
import BlockContent from '@sanity/block-content-to-react'

function BlogPost({ data }) {
  return (
    <article>
      <h1>{data.sanityPost.title}</h1>
      <BlockContent 
        blocks={data.sanityPost._rawBody}
        projectId="your-project-id"
        dataset="your-dataset"
      />
    </article>
  )
}

Custom Components

Both packages support custom serializers/components for rendering custom blocks, marks, and types:

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

<PortableText value={data._rawBody} components={components} />

Learning Resources

While I couldn't find the specific video you're looking for, these resources might help:

The core concepts are the same across React frameworks—you're rendering Portable Text blocks that come through as JSON from your GraphQL query. The main thing to remember is to use the _raw prefix in your Gatsby queries!

Show original thread
13 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?