fix(react): useEditor hook now respects deps array and will re-initialize editor (#5353)

This commit is contained in:
Matic Zavadlal 2024-07-17 13:04:39 +02:00 committed by GitHub
parent a473826eb1
commit 70eebfdb0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -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

View File

@ -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)