Skip to content

fix: react 19 strictmode race condition #1196

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

Merged
merged 1 commit into from
Nov 6, 2024
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
9 changes: 8 additions & 1 deletion packages/core/src/editor/BlockNoteTipTapEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type BlockNoteTipTapEditorOptions = Partial<
// @ts-ignore
export class BlockNoteTipTapEditor extends TiptapEditor {
private _state: EditorState;

private _creating = false;
public static create = (
options: BlockNoteTipTapEditorOptions,
styleSchema: StyleSchema
Expand Down Expand Up @@ -151,8 +151,12 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
* Replace the default `createView` method with a custom one - which we call on mount
*/
private createViewAlternative() {
this._creating = true;
// Without queueMicrotask, custom IC / styles will give a React FlushSync error
queueMicrotask(() => {
if (!this._creating) {
return;
}
this.view = new EditorView(
{ mount: this.options.element as any }, // use mount option so that we reuse the existing element instead of creating a new one
{
Expand All @@ -178,6 +182,7 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
this.commands.focus(this.options.autofocus);
this.emit("create", { editor: this });
this.isInitialized = true;
this._creating = false;
});
}

Expand All @@ -189,6 +194,8 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
public mount = (element?: HTMLElement | null) => {
if (!element) {
this.destroy();
// cancel pending microtask
this._creating = false;
} else {
this.options.element = element;
// @ts-ignore
Expand Down
Loading