|
1 | 1 | # @july_cm/react-notion |
2 | 2 |
|
| 3 | +React 版本的 notion 渲染器,支持 React Server Components([RSC](https://react.dev/reference/rsc/server-components)) 渲染。 |
| 4 | + |
| 5 | +## 安装 |
| 6 | + |
| 7 | +``` |
| 8 | +npm install @july_cm/react-notion |
| 9 | +``` |
| 10 | + |
| 11 | +仅完全支持 **React v19** 及以上版本,更低版本的 React 不保证其兼容性。 |
| 12 | + |
| 13 | +## RSC 开箱即用 |
| 14 | + |
| 15 | +在 RSC 架构下,可以直接使用 `@july_cm/react-notion` 透传的 Client,请务必保证 `client` 实例仅运行在 **server-only** 环境已防止 token 泄露。 |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { Notion, NotionRenderProvider, Client } from '@july_cm/react-notion'; |
| 19 | + |
| 20 | +function Page(id: string) { |
| 21 | + const client = new Client({ auth: process.env.NOTION_TOKEN }) |
| 22 | + |
| 23 | + return ( |
| 24 | + <NotionRenderProvider components={{ |
| 25 | + paragraph: Paragraph |
| 26 | + }}> |
| 27 | + <Notion blockId={id} client={client} /> |
| 28 | + </NotionRenderProvider> |
| 29 | + ) |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## RCC 环境 |
| 34 | + |
| 35 | +RCC 并非本包主力支持的用法(也许未来支持),但也能做到一定程度的使用。 |
| 36 | + |
| 37 | +需要注意的是,在这种设计下应当有其私有的服务来封装 notion SDK。 |
| 38 | + |
| 39 | +```tsx |
| 40 | +import React, { useState, useEffect } from "react"; |
| 41 | +import { Render, NotionRenderProvider, Client } from '@july_cm/react-notion'; |
| 42 | + |
| 43 | +async function Page(id: string) { |
| 44 | + const [data, setData] = useState([]); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + const result = await customApi() |
| 48 | + setData(data); |
| 49 | + }, [id]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <NotionRenderProvider components={{ |
| 53 | + paragraph: Paragraph |
| 54 | + }}> |
| 55 | + <Render data={data} /> |
| 56 | + </NotionRenderProvider> |
| 57 | + ) |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## 自定义组件 |
| 62 | + |
| 63 | +`@july_cm/react-notion` 本身不提供任何物料,notion 的各类 Block 都需要自行实现 |
| 64 | + |
| 65 | +```ts |
| 66 | +export interface CustomComponentProps<T> { |
| 67 | + block: T; |
| 68 | + children?: React.ReactNode; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * 自定义组件 |
| 73 | + */ |
| 74 | +export type CustomComponent<T> = React.FC<CustomComponentProps<T>>; |
| 75 | +``` |
3 | 76 |
|
4 | 77 | ## 工具函数 |
5 | 78 |
|
|
0 commit comments