From 70eebfdb0ccb8a0a5f84425a7dc17c75accde0fb Mon Sep 17 00:00:00 2001 From: Matic Zavadlal Date: Wed, 17 Jul 2024 13:04:39 +0200 Subject: [PATCH] fix(react): useEditor hook now respects deps array and will re-initialize editor (#5353) --- .changeset/curly-buses-attend.md | 5 +++++ packages/react/src/useEditor.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/curly-buses-attend.md diff --git a/.changeset/curly-buses-attend.md b/.changeset/curly-buses-attend.md new file mode 100644 index 000000000..d797b0ea5 --- /dev/null +++ b/.changeset/curly-buses-attend.md @@ -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 diff --git a/packages/react/src/useEditor.ts b/packages/react/src/useEditor.ts index 5df40e60c..6f1f64bbb 100644 --- a/packages/react/src/useEditor.ts +++ b/packages/react/src/useEditor.ts @@ -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)