Education
Developer Advocate
Last updated onNov 2, 2023
Last updated onNov 2, 2023
Sanity CMS is a platform used by developers to structure content. It is not open source, but it offers a generous free tier. It's built on React, a popular JavaScript library for building user interfaces, particularly single-page applications. React allows developers to create large web applications that update and render efficiently in response to data changes.
React has a concept of components, reusable pieces of code that return a React element to be rendered to the page. The smallest React component could be an in-place function that returns some JSX.
1function Hello() { 2 return <h3>Hello, world!</h3>; 3} 4
In this blog post, we will focus on the @sanity/block-content-to-react package, which is a powerful tool for transforming Sanity block content to React components. This package is particularly useful when rendering rich text content from Sanity in your React application.
In React, a block component is a reusable piece of code that can be considered a JavaScript function that returns a React element. The block component in React is used to encapsulate and manage the behavior of a particular piece of the user interface.
Sanity CMS uses a block content structure for its rich text fields. This structure is a tree-like structure of blocks, where each block can contain other blocks. The @sanity/block-content-to-react package provides a way to transform this block content into React components.
1import BlockContent from '@sanity/block-content-to-react' 2 3function MyComponent(props) { 4 return ( 5 <BlockContent blocks={props.blocks} /> 6 ) 7} 8
In the above code snippet, BlockContent is a React component that takes a blocks prop. This blocks prop is an array of block objects from Sanity. The BlockContent component transforms these blocks into React elements.
Portable Text is a JSON-based rich text specification for modern content editing platforms. It allows for representing rich text content in a structured and platform-agnostic way. In the context of Sanity and React, Portable Text represents the block content structure of Sanity's rich text fields.
The @sanity/block-content-to-react package provides a PortableText component that can be used to render Portable Text in React.
1import PortableText from '@sanity/block-content-to-react' 2 3function MyComponent(props) { 4 return ( 5 <PortableText blocks={props.blocks} /> 6 ) 7} 8
In the above code snippet, PortableText is a React component that takes a blocks prop. This blocks prop is an array of block objects from Sanity. The PortableText component transforms these blocks into React elements.
The @sanity/block-content-to-react package provides a way to transform Sanity block content to React components. This transformation is done using serializers. A serializer is a function that takes a block and returns a React element.
The package provides a set of default serializers for common block types like headings, paragraphs, and links. However, you can also provide your custom serializers for more complex block types or to customize the rendering of the default block types.
1import BlockContent from '@sanity/block-content-to-react' 2 3const serializers = { 4 types: { 5 block: (props) => { 6 switch (props.node.style) { 7 case 'h1': 8 return <h3>{props.children}</h3> 9 case 'h2': 10 return <h3>{props.children}</h3> 11 default: 12 return <p>{props.children}</p> 13 } 14 } 15 } 16} 17 18function MyComponent(props) { 19 return ( 20 <BlockContent blocks={props.blocks} serializers={serializers} /> 21 ) 22} 23
In the above code snippet, a custom serializer is defined for the block type. This serializer checks the style of the block and returns a different React element depending on the style.
When transforming Sanity block content to React, you may encounter unknown block types. The default serializers do not handle these block types and for which you have not provided a custom serializer.
The @sanity/block-content-to-react package provides a way to handle these unknown block types. You can provide a fallback serializer that will be used for any block types not handled by the other serializers.
1import BlockContent from '@sanity/block-content-to-react' 2 3const serializers = { 4 types: { 5 block: (props) => {/* ... */}, 6 fallback: (props) => { 7 console.warn('Unknown block type', props.node._type) 8 return null 9 } 10 } 11} 12 13function MyComponent(props) { 14 return ( 15 <BlockContent blocks={props.blocks} serializers={serializers} /> 16 ) 17} 18
In the above code snippet, a fallback serializer is defined that logs a warning message and returns null, effectively ignoring the unknown block type.
React Native is a framework for building mobile applications using React. It provides a set of built-in components that are similar to HTML elements but are designed to work in a mobile environment.
React Native elements can be used when transforming Sanity block content to React. This can be useful if you are building a mobile application and want to render Sanity block content.
1import BlockContent from '@sanity/block-content-to-react' 2import { Text } from 'react-native' 3 4const serializers = { 5 types: { 6 block: (props) => <Text>{props.children}</Text> 7 } 8} 9 10function MyComponent(props) { 11 return ( 12 <BlockContent blocks={props.blocks} serializers={serializers} /> 13 ) 14} 15
In the above code snippet, the Text component from React Native is used in the serializer for the block type. This allows the block content to be rendered in a mobile application.
Newline characters in text can be a bit tricky to handle in React. By default, newline characters in text are not rendered as line breaks in HTML. However, there are situations where you might want to render newline characters as line breaks, for example when displaying preformatted text or code.
The @sanity/block-content-to-react package provides a way to handle newline characters when transforming Sanity block content to React. You can provide a custom serializer for the hardBreak type, which represents newline characters in Sanity block content.
1import BlockContent from '@sanity/block-content-to-react' 2 3const serializers = { 4 marks: { 5 hardBreak: () => <br /> 6 } 7} 8 9function MyComponent(props) { 10 return ( 11 <BlockContent blocks={props.blocks} serializers={serializers} /> 12 ) 13} 14
In the above code snippet, a custom serializer is defined for the hardBreak type. This serializer returns a
element, which is the HTML element used to represent a line break.
Images are a common type of content in Sanity block content. The @sanity/block-content-to-react package provides a default serializer for image blocks, but you can also provide a custom serializer if you want to customize how images are rendered.
1import BlockContent from '@sanity/block-content-to-react' 2import imageUrlBuilder from '@sanity/image-url' 3 4const builder = imageUrlBuilder(sanityClient) 5 6function urlFor(source) { 7 return builder.image(source) 8} 9 10const serializers = { 11 types: { 12 image: (props) => <img src={urlFor(props.node.asset).url()} /> 13 } 14} 15 16function MyComponent(props) { 17 return ( 18 <BlockContent blocks={props.blocks} serializers={serializers} /> 19 ) 20} 21
In the above code snippet, a custom serializer is defined for the image type. This serializer uses the @sanity/image-url package to build the URL for the image, then renders an element with this URL as the src attribute.
The @sanity/block-content-to-react package provides a set of built-in serializers for common block types like headings, paragraphs, and links. These built-in serializers provide a good starting point, but you can also provide your custom serializers to customize the rendering of these block types.
1import BlockContent from '@sanity/block-content-to-react' 2 3const serializers = { 4 types: { 5 block: (props) => { 6 switch (props.node.style) { 7 case 'h1': 8 return <h3 className="my-custom-class">{props.children}</h3> 9 case 'h2': 10 return <h3 className="my-custom-class">{props.children}</h3> 11 default: 12 return <p>{props.children}</p> 13 } 14 } 15 } 16} 17 18function MyComponent(props) { 19 return ( 20 <BlockContent blocks={props.blocks} serializers={serializers} /> 21 ) 22} 23
In the above code snippet, a custom serializer is defined for the block type. This serializer adds a custom CSS class to the rendered headings.
Query parameters are a powerful tool in React that can be used to control the behavior of components. In the context of transforming Sanity block content to React, query parameters can be used to control the behavior of the BlockContent component.
For example, you can use a query parameter to control whether the BlockContent component should render newline characters as line breaks.
1import BlockContent from '@sanity/block-content-to-react' 2 3function MyComponent(props) { 4 const renderNewlines = new URLSearchParams(window.location.search).get('renderNewlines') 5 6 const serializers = { 7 marks: { 8 hardBreak: renderNewlines ? () => <br /> : undefined 9 } 10 } 11 12 return ( 13 <BlockContent blocks={props.blocks} serializers={serializers} /> 14 ) 15} 16
In the above code snippet, the renderNewlines query parameter controls whether the hardBreak serializer should be defined. If the renderNewlines query parameter is true, newline characters will be rendered as line breaks. Otherwise, newline characters will be rendered as spaces.
In conclusion, the @sanity/block-content-to-react package is a powerful tool for transforming Sanity block content to React components. It provides a set of default serializers for common block types, and allows you to provide your custom serializers for more complex block types or to customize the rendering of the default block types.
Whether you're building a web application with React or a mobile application with React Native, this package can help you render Sanity block content that fits your needs. And with the power of query parameters, you can even control the behavior of the BlockContent component based on the URL of the page.
So whether you're a seasoned React developer or just getting started, I hope this blog post has given you a deeper understanding of how to work with Sanity block content in React. Happy coding!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.