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

Avoid flushSync errors in useEffect #48

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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