Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/piotrzaborow/notion-blocks-react-renderer

React Renderer for @notionhq/client blocks
https://github.com/piotrzaborow/notion-blocks-react-renderer

notion notion-renderer react

Last synced: 24 days ago
JSON representation

React Renderer for @notionhq/client blocks

Awesome Lists containing this project

README

        

# Notion Blocks React Renderer

React Renderer for [@notionhq/client](https://www.npmjs.com/package/@notionhq/client) blocks

## How to install?

```
npm install notion-blocks-react-renderer
```

or

```
yarn add notion-blocks-react-renderer
```

## How to use Notion Blocks React Renderer?

```

import { blocksToReactComponents } from 'notion-blocks-react-renderer'

const Component = () => {
const [blocks, setBlocks] = useState([])
useEffect(() => {
const blocks = fetchFromNotionAPI()
},[])

return <>
{blocksToReactComponents(blocks)}
>
}
```

You can add your own renderers (Components) for Notion Blocks by creating new object and passing it to `blocksToReactComponents` function as a second parameter:

```
import { blockToText, blocksToReactComponents, BLOCKS, blockRenderer } from 'notion-blocks-react-renderer'

const renderers = {
[BLOCKS.PARAGRAPH]: (block) =>

{ blockToText(block) }

,
[BLOCKS.HEADING1]: (block) =>

{ blockToText(block) }

,
[BLOCKS.HEADING2]: (block) =>

{ blockToText(block) }

,
[BLOCKS.HEADING3]: (block) =>

{ blockToText(block) }

,
[BLOCKS.BULLETEDLISTITEM]: (block) =>
  • { blockToText(block) }
  • ,
    [BLOCKS.BULLETEDLIST]: (block) =>
      { block.list.map((listItem) => blockRenderer(listItem, renderers)) }
    ,
    [BLOCKS.NUMBEREDLISTITEM]: (block) =>
  • { blockToText(block) }
  • ,
    [BLOCKS.NUMBEREDLIST]: (block) =>
      { block.list.map((listItem) => blockRenderer(listItem, renderers)) }
    ,
    [BLOCKS.QUOTE]: (block) =>
    { blockToText(block) }
    ,
    [BLOCKS.CODE]: (block) => { blockToText(block) },
    [BLOCKS.IMAGE]: (block) => ,
    [BLOCKS.AUDIO]: (block) =>
    }

    const Component = () => {
    const [blocks, setBlocks] = useState([])
    useEffect(() => {
    const blocks = fetchFromNotionAPI()
    },[])

    return <>
    {blocksToReactComponents(blocks, renderers)}
    >
    }

    ```