[Bug]: useEditor can hand a component an editor that is destroyed before its first effect runs (lazy/Suspense)
Affected Packages
@tiptap/react
Tiptap Version
3.26.1 (same code in 3.31.3)
Browser Used
Not browser-specific
What happened?
EditorInstanceManager's constructor creates the editor during render (immediatelyRender defaults to true) and immediately calls scheduleDestroy(), a setTimeout(…, 1) that destroys the editor if isComponentMounted is still false. When the render → passive-effect gap exceeds 1ms — reliably when the component is React.lazy-loaded inside <Suspense> (the retry commit) — the timer fires first: editor.destroy() resets extensionStorage to {} and a replacement editor is created on mount. But the component's own useEffects still run once with the destroyed editor from the render closure, so any effect reading editor.storage.<ext> throws, e.g. with tiptap-markdown:
TypeError: Cannot read properties of undefined (reading 'getMarkdown')
Minimal repro (vitest/jsdom, must be outside act() since act flushes effects synchronously):
const Comp = ({ value }: { value: string }) => {
const editor = useEditor({ extensions: [StarterKit, Markdown], content: value });
useEffect(() => {
if (!editor) return;
(editor.storage as any).markdown.getMarkdown(); // throws on the first run
}, [editor]);
return editor ? <EditorContent editor={editor} /> : null;
};
const Lazy = lazy(() => new Promise((r) => setTimeout(r, 20)).then(() => ({ default: Comp })));
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = false;
createRoot(el).render(<Suspense fallback={null}><Lazy value="**hi**" /></Suspense>);Fails every run; the same component imported eagerly never fails.
Expected Behavior
useEditor should never return an editor to a committed render that it has already destroyed — e.g. don't destroy an editor created during render until the component has had a chance to mount (or re-check before returning it), or document that consumers must guard editor.isDestroyed in effects.
Additional Context (Optional)
Workaround: if (!editor || editor.isDestroyed) return; in effects — the replacement editor re-renders the component and the effect re-runs with it.
Source: ueberdosis/tiptap