|
| 1 | +--- |
| 2 | +title: Next.js and BlockNote |
| 3 | +description: Details on integrating BlockNote with Next.js |
| 4 | +imageTitle: Next.js and BlockNote |
| 5 | +path: /docs/nextjs |
| 6 | +--- |
| 7 | + |
| 8 | +# Next.js and BlockNote |
| 9 | + |
| 10 | +BlockNote is a component that should only be rendered client-side (and not on the server). If you're using Next.js, you need to make sure that Next.js does not try to render BlockNote as a server-side component. |
| 11 | + |
| 12 | +To do this, first see if you're using the modern [App router](https://nextjs.org/docs/app) or the classic [Pages router](https://nextjs.org/docs/pages). |
| 13 | + |
| 14 | +(If the component you want to add BlockNote to is in an `app` directory, you're likely to be using the App router. If you're working in a `pages` directory, you're using the pages router). |
| 15 | + |
| 16 | +## App router |
| 17 | + |
| 18 | +Make sure to use BlockNote in a [Client Component](https://nextjs.org/docs/getting-started/react-essentials#client-components). You can do this by creating a separate file for your component, and starting that with `"use client";` [directive](https://react.dev/reference/react/use-client). |
| 19 | + |
| 20 | +```typescript |
| 21 | +"use client"; // this registers <Editor> as a Client Component |
| 22 | +import { BlockNoteEditor } from "@blocknote/core"; |
| 23 | +import { BlockNoteView, useBlockNote } from "@blocknote/react"; |
| 24 | +import "@blocknote/core/style.css"; |
| 25 | + |
| 26 | +// Our <Editor> component that we can now use |
| 27 | +export default Editor() { |
| 28 | + // Creates a new editor instance. |
| 29 | + const editor: BlockNoteEditor | null = useBlockNote({}); |
| 30 | + |
| 31 | + // Renders the editor instance using a React component. |
| 32 | + return <BlockNoteView editor={editor} />; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Pages router |
| 37 | + |
| 38 | +If you're using the classic Pages router (note that Next.js recommends upgrading to the App router) and are running into issues embedding BlockNote directly, you can use [Dynamic Imports](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading) to make sure BlockNote is only imported on the client-side. |
| 39 | + |
| 40 | +First, create an isolated `<Editor>` component using the snipped above. |
| 41 | + |
| 42 | +Then, you can import this using `next/dynamic` in your page: |
| 43 | + |
| 44 | +```typescript |
| 45 | +import dynamic from "next/dynamic"; |
| 46 | + |
| 47 | +const Editor = dynamic(() => import("./editor"), { ssr: false }); |
| 48 | + |
| 49 | +function App() { |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <Editor /> |
| 53 | + </div> |
| 54 | + ); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +This should resolve any issues you might run into when embedding BlockNote in your Next.js React app! |
0 commit comments