Skip to content

Commit

Permalink
In a README examples, create the initial editor state only once (#140)
Browse files Browse the repository at this point in the history
The code examples use react state with this example:

```
const [state, setState] = useState(EditorState.create({ schema }));
```

With those examples, on every render, `EditorState.create` will be called.

To avoid that, we can use an initializer function ([documentation](https://react.dev/reference/react/useState#avoiding-recreating-the-initial-state)). In our case, we replace with:

```
const [state, setState] = useState(() => EditorState.create({ schema }));
```
  • Loading branch information
alexandresalome authored Oct 8, 2024
1 parent 6adcf79 commit 9d50d24
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ import { ProseMirror } from "@nytimes/react-prosemirror";

export function ProseMirrorEditor() {
const [mount, setMount] = useState<HTMLElement | null>(null);
const [state, setState] = useState(EditorState.create({ schema }));
const [state, setState] = useState(() => EditorState.create({ schema }));

return (
<ProseMirror
Expand Down Expand Up @@ -187,7 +187,7 @@ import { SelectionWidget } from "./SelectionWidget.tsx";

export function ProseMirrorEditor() {
const [mount, setMount] = useState<HTMLElement | null>(null);
const [state, setState] = useState(EditorState.create({ schema }))
const [state, setState] = useState(() => EditorState.create({ schema }))

return (
<ProseMirror
Expand Down Expand Up @@ -242,7 +242,7 @@ import { BoldButton } from "./BoldButton.tsx";

export function ProseMirrorEditor() {
const [mount, setMount] = useState<HTMLElement | null>(null);
const [state, setState] = useState(EditorState.create({ schema }));
const [state, setState] = useState(() => EditorState.create({ schema }));

return (
<ProseMirror
Expand Down

0 comments on commit 9d50d24

Please sign in to comment.