Skip to content

Commit

Permalink
Avoid flushSync errors in useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdemartini committed Jun 16, 2023
1 parent d125336 commit 651f5e1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ const RichTextEditor = forwardRef<RichTextEditorRef, RichTextEditorProps>(
if (!editor || editor.isDestroyed || editor.isEditable === editable) {
return;
}
editor.setEditable(editable);
// We use queueMicrotask to avoid any flushSync console errors as
// mentioned here (though setEditable shouldn't trigger them in practice)
// https://github.com/ueberdosis/tiptap/issues/3764#issuecomment-1546854730
queueMicrotask(() => editor.setEditable(editable));
}, [editable, editor]);

// Update content if/when it changes
Expand All @@ -110,7 +113,15 @@ const RichTextEditor = forwardRef<RichTextEditorRef, RichTextEditorProps>(
) {
return;
}
editor.commands.setContent(editorProps.content);
// We use queueMicrotask to avoid any flushSync console errors as
// mentioned here
// https://github.com/ueberdosis/tiptap/issues/3764#issuecomment-1546854730
queueMicrotask(() => {
// Validate that editorProps.content isn't undefined again to appease TS
if (editorProps.content !== undefined) {
editor.commands.setContent(editorProps.content);
}
});
}, [editorProps.content, editor]);

useEffect(() => {
Expand Down

0 comments on commit 651f5e1

Please sign in to comment.