Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/thick-months-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tiptap/react': patch
---

Fix race conditions in ReactRenderer causing destroyed renderers to be re-added in Strict Mode
17 changes: 17 additions & 0 deletions packages/react/src/ReactRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ export class ReactRenderer<R = unknown, P extends Record<string, any> = object>

ref: R | null = null

/**
* Flag to track if the renderer has been destroyed, preventing queued or asynchronous renders from executing after teardown.
*/
destroyed = false

/**
* Immediately creates element and renders the provided React component.
*/
Expand Down Expand Up @@ -186,6 +191,9 @@ export class ReactRenderer<R = unknown, P extends Record<string, any> = object>
})
} else {
queueMicrotask(() => {
if (this.destroyed) {
return
}
this.render()
})
}
Expand All @@ -195,6 +203,10 @@ export class ReactRenderer<R = unknown, P extends Record<string, any> = object>
* Render the React component.
*/
render(): void {
if (this.destroyed) {
return
}

const Component = this.component
const props = this.props
const editor = this.editor as EditorWithContentComponent
Expand Down Expand Up @@ -227,6 +239,10 @@ export class ReactRenderer<R = unknown, P extends Record<string, any> = object>
* Re-renders the React component with new props.
*/
updateProps(props: Record<string, any> = {}): void {
if (this.destroyed) {
return
}

this.props = {
...this.props,
...props,
Expand All @@ -239,6 +255,7 @@ export class ReactRenderer<R = unknown, P extends Record<string, any> = object>
* Destroy the React component.
*/
destroy(): void {
this.destroyed = true
const editor = this.editor as EditorWithContentComponent

editor?.contentComponent?.removeRenderer(this.id)
Expand Down
Loading