Skip to content
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

Ref for editor gets executed on every render #260

Closed
firatoezcan opened this issue Aug 21, 2024 · 0 comments · Fixed by #261
Closed

Ref for editor gets executed on every render #260

firatoezcan opened this issue Aug 21, 2024 · 0 comments · Fixed by #261

Comments

@firatoezcan
Copy link
Contributor

Describe the bug

The ref function passed into RichTextEditor gets executed on every render. This makes it not possible to use ref callbacks for focusing the editor and forces us to use useEffect.

This is the line that is responsible for it: https://github.com/sjdemartini/mui-tiptap/blob/main/src/RichTextEditor.tsx#L104

There is no dependency array for useImperativeHandle.

To Reproduce

Add this function below outside of the react component (so its stable) and add it as ref

const focus = (el: RichTextEditorRef) => {
  console.log("Focus")
  el?.editor?.commands.focus()
}

Console is spammed with Focus. This f.e. makes it impossible to add a link since the focus will be stolen all the time

Expected behavior

ref callback is only executed once (maybe a few times, but only at the start or when the editor changes)

Fix

diff --git a/dist/esm/RichTextEditor.js b/dist/esm/RichTextEditor.js
index 09efe429431fcbd1217eb775954dbaaadb9717fa..c4bfbc7be4e57f32065ad6f1b790536b9b4992d3 100644
--- a/dist/esm/RichTextEditor.js
+++ b/dist/esm/RichTextEditor.js
@@ -26,7 +26,7 @@ editable = true, ...editorProps }, ref) {
     // Allow consumers of this component to access the editor via ref
     useImperativeHandle(ref, () => ({
         editor: editor,
-    }));
+    }), [editor]);
     // Update editable state if/when it changes
     useEffect(() => {
         if (!editor || editor.isDestroyed || editor.isEditable === editable) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant