PortableText content prop error in Next.js after publishing to Sanity

13 replies
Last updated: Aug 12, 2022
I'm struggling with PortableText components content prop in react/next.js. I get this error every time i publish content to the sanity cms editor and try and load the post on my local host.
my nextjs code


<PortableText
            className='mt-10'
            content={post.body}
            dataset={process.env.NEXT_PUBLIC_SANITY_DATASET}
            projectId={process.env.NEXT_PUBLIC_SANITY_PROJECT_ID}
            serializers={{
              h1: (props: any) => (
                <h1 className='text-2xl font-bold my-5'>{props}</h1>
              ),
              h2: (props: any) => (
                <h2 className='text-xl font-bold my-5'>{props}</h2>
              ),
              li: (props: any) => <li className='ml-4 list-disc'>{props}</li>,
              link: ({ href, children }: any) => (
                <a className='text-blue-500 hover:underline' href={href}>
                  {children}
                </a>
              ),
            }}
          />

AI Update

Looking at your code, I can see the issue! You're using the old deprecated @sanity/block-content-to-react package API with serializers and content props, but it looks like you've upgraded to the newer @portabletext/react package which has a different API.

The @sanity/block-content-to-react package was deprecated in favor of @portabletext/react, and the new package uses different prop names:

  • content โ†’ value
  • serializers โ†’ components
  • You no longer need dataset and projectId props

Here's how to update your code:

import { PortableText } from '@portabletext/react'

<PortableText
  className='mt-10'
  value={post.body}  // Changed from 'content' to 'value'
  components={{      // Changed from 'serializers' to 'components'
    block: {
      h1: ({children}) => (
        <h1 className='text-2xl font-bold my-5'>{children}</h1>
      ),
      h2: ({children}) => (
        <h2 className='text-xl font-bold my-5'>{children}</h2>
      ),
    },
    list: {
      bullet: ({children}) => <ul className='ml-4'>{children}</ul>,
    },
    listItem: {
      bullet: ({children}) => <li className='list-disc'>{children}</li>,
    },
    marks: {
      link: ({value, children}) => (
        <a className='text-blue-500 hover:underline' href={value?.href}>
          {children}
        </a>
      ),
    },
  }}
/>

Key changes to note:

  1. Props structure: The components receive children instead of being passed the entire props object
  2. Organization: Components are organized into block, list, listItem, and marks categories
  3. Links: Links are now under marks.link and receive a value prop with the link data

Make sure you have @portabletext/react installed:

npm install @portabletext/react

You can find more details in the official Sanity documentation on presenting Portable Text, which covers the migration from the old package to the new one.

Show original thread
13 replies
First suggestion would be to move away from the deprecated portable text renderer. ๐Ÿ™‚ This is the new supported way: https://www.sanity.io/docs/portable-text-to-react
thank you!
If you still have the error then, we can check. ๐Ÿ™‚
Iโ€™m also pretty sure that it should be
{props.children}
and not hust
{props}
? (that is, the approach you have for the
link
serializer)
Ah yes, what Knut said is the actual problem.
But still I encourage you to migrate. ๐Ÿ˜„
I have migrated and everything works, thank you for the quick response. the only thing im stuck on now is styling 'normal; text. im trying to add y axis padding
im not having success with this


 p: ({ children }: any) => <p className='font-bold my-2'>{children}</p>,
can anyone help with this?
p
is not a recognized prop in Portable Text. What you want to customize is
components.block.normal
.
where can i find that in docs,i must of missed it
Mentioned here: https://github.com/portabletext/react-portabletext#block ๐Ÿ™‚
components={{
  block: {
    normal: ({children}) => <p>{children}</p>
  }
}}
totally missed that, i think that's a sign of i need a break. thank you.
๐Ÿ’š

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?