-
-
Notifications
You must be signed in to change notification settings - Fork 558
docs: nextjs guidance #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: Next.js and BlockNote | ||
description: Details on integrating BlockNote with Next.js | ||
imageTitle: Next.js and BlockNote | ||
path: /docs/nextjs | ||
--- | ||
|
||
# Next.js and BlockNote | ||
|
||
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. | ||
|
||
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). | ||
|
||
(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). | ||
|
||
## App router | ||
|
||
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). | ||
|
||
```typescript | ||
"use client"; // this registers <Editor> as a Client Component | ||
import { BlockNoteEditor } from "@blocknote/core"; | ||
import { BlockNoteView, useBlockNote } from "@blocknote/react"; | ||
import "@blocknote/core/style.css"; | ||
|
||
// Our <Editor> component that we can now use | ||
export default Editor() { | ||
// Creates a new editor instance. | ||
const editor: BlockNoteEditor | null = useBlockNote({}); | ||
|
||
// Renders the editor instance using a React component. | ||
return <BlockNoteView editor={editor} />; | ||
} | ||
``` | ||
|
||
## Pages router | ||
|
||
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. | ||
|
||
First, create an isolated `<Editor>` component using the snipped above. | ||
|
||
Then, you can import this using `next/dynamic` in your page: | ||
|
||
```typescript | ||
import dynamic from "next/dynamic"; | ||
|
||
const Editor = dynamic(() => import("./editor"), { ssr: false }); | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<Editor /> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
This should resolve any issues you might run into when embedding BlockNote in your Next.js React app! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.