Skip to content

Commit

Permalink
fix(react): useEditor hook now respects deps array and will re-initia…
Browse files Browse the repository at this point in the history
…lize editor (#5353)
  • Loading branch information
maticzav authored Jul 17, 2024
1 parent a473826 commit 70eebfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curly-buses-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/react": patch
---

The optional deps argument to useEditor was not being respected for performance optimizations, now if deps are declared a new editor instance is created
8 changes: 7 additions & 1 deletion packages/react/src/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ export function useEditor(
editorInstance = new Editor(options)
// instantiate the editor if it doesn't exist
// for ssr, this is the first time the editor is created
setEditor(editorInstance)
} else if (Array.isArray(deps) && deps.length) {
// the deps array is used to re-initialize the editor instance
editorInstance = new Editor(options)

setEditor(editorInstance)
} else {
// if the editor does exist, update the editor options accordingly
// if the editor does exist & deps are empty, we don't need to re-initialize the editor
// we can fast-path to update the editor options on the existing instance
editorInstance.setOptions(options)
}
}, deps)
Expand Down

0 comments on commit 70eebfd

Please sign in to comment.