Easily render the content of Strapi's new Blocks rich text editor in your React frontend.
npm install @strapi/blocks-react-rendererEasily render the content of Strapi's new Blocks rich text editor in your React frontend.
Install the Blocks renderer and its peer dependencies:
``sh`
yarn add @strapi/blocks-react-renderer react react-dom
`sh`
npm install @strapi/blocks-react-renderer react react-dom
After fetching your Strapi content, you can use the BlocksRenderer component to render the data from a blocks attribute. Pass the array of blocks coming from your Strapi API to the content prop:
`jsx
import { BlocksRenderer, type BlocksContent } from '@strapi/blocks-react-renderer';
// Content should come from your Strapi API
const content: BlocksContent = [
{
type: 'paragraph',
children: [{ type: 'text', text: 'A simple paragraph' }],
},
];
const App = () => {
return
};
`
You can provide your own React components to the renderer, both for blocks and modifier. They will be merged with the default components, so you can override only the ones you need.
- Blocks are full-width elements, usually at the root of the content. The available options are:
- paragraph
- heading (receives level)format
- list (receives )plainText
- quote
- code (receives )image
- image (receives )url
- link (receives )
- Modifiers are inline elements, used to change the appearance of fragments of text within a block. The available options are:
- bold
- italic
- underline
- strikethrough
- code
To provide your own components, pass an object to the blocks and modifiers props of the renderer. For each type, the value should be a React component that will receive the props of the block or modifier. Make sure to always render the children, so that the nested blocks and modifiers are rendered as well.
`jsx
import { BlocksRenderer } from '@strapi/blocks-react-renderer';
// Content should come from your Strapi API
const content = [
{
type: 'paragraph',
children: [{ type: 'text', text: 'A simple paragraph' }],
},
];
const App = () => {
return (
blocks={{
// You can use the default components to set class names...
paragraph: ({ children }) =>
{children}
,