Skip to content

Commit 8aa89ab

Browse files
authored
fix: react 19 strictmode race condition (#1196)
1 parent 2808dd6 commit 8aa89ab

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/core/src/editor/BlockNoteTipTapEditor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type BlockNoteTipTapEditorOptions = Partial<
2424
// @ts-ignore
2525
export class BlockNoteTipTapEditor extends TiptapEditor {
2626
private _state: EditorState;
27-
27+
private _creating = false;
2828
public static create = (
2929
options: BlockNoteTipTapEditorOptions,
3030
styleSchema: StyleSchema
@@ -151,8 +151,12 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
151151
* Replace the default `createView` method with a custom one - which we call on mount
152152
*/
153153
private createViewAlternative() {
154+
this._creating = true;
154155
// Without queueMicrotask, custom IC / styles will give a React FlushSync error
155156
queueMicrotask(() => {
157+
if (!this._creating) {
158+
return;
159+
}
156160
this.view = new EditorView(
157161
{ mount: this.options.element as any }, // use mount option so that we reuse the existing element instead of creating a new one
158162
{
@@ -178,6 +182,7 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
178182
this.commands.focus(this.options.autofocus);
179183
this.emit("create", { editor: this });
180184
this.isInitialized = true;
185+
this._creating = false;
181186
});
182187
}
183188

@@ -189,6 +194,8 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
189194
public mount = (element?: HTMLElement | null) => {
190195
if (!element) {
191196
this.destroy();
197+
// cancel pending microtask
198+
this._creating = false;
192199
} else {
193200
this.options.element = element;
194201
// @ts-ignore

0 commit comments

Comments
 (0)