https://github.com/strapi/blocks-react-renderer
A React renderer for the Strapi's Blocks rich text editor
https://github.com/strapi/blocks-react-renderer
Last synced: about 1 year ago
JSON representation
A React renderer for the Strapi's Blocks rich text editor
- Host: GitHub
- URL: https://github.com/strapi/blocks-react-renderer
- Owner: strapi
- License: other
- Created: 2023-10-12T08:39:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-10T15:29:22.000Z (about 1 year ago)
- Last Synced: 2025-04-21T18:45:50.598Z (about 1 year ago)
- Language: TypeScript
- Size: 1.18 MB
- Stars: 199
- Watchers: 14
- Forks: 32
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Strapi Blocks React Renderer
Easily render the content of Strapi's new Blocks rich text editor in your React frontend.
## Installation
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
```
## Basic usage
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 ;
};
```
## Custom components
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` and `plainText`)
- list (receives `format`)
- quote
- code (receives `plainText`)
- image (receives `image`)
- link (receives `url`)
- **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 (
{children}
,
// ...or point to a design system
heading: ({ children, level }) => {
switch (level) {
case 1:
return {children}
case 2:
return {children}
case 3:
return {children}
case 4:
return {children}
case 5:
return {children}
case 6:
return {children}
default:
return {children}
}
},
// For links, you may want to use the component from your router or framework
link: ({ children, url }) => {children},
}}
modifiers={{
bold: ({ children }) => {children},
italic: ({ children }) => {children},
}}
/>
);
};
```