Skip to content

Commit

Permalink
fix: react 19 strictmode race condition (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefED authored Nov 6, 2024
1 parent 2808dd6 commit 8aa89ab
Showing 1 changed file with 8 additions and 1 deletion.
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

0 comments on commit 8aa89ab

Please sign in to comment.