tiptap: [Bug]: Editor options set on every render
Affected Packages
react
Version(s)
2.11.2
Bug Description
editor.setOptions is being called on each render in case of empty deps.
The bug I ran into is quite obscure but there could be more problems.
Scenario:
const ExampleExtension = Extension.create({
addProseMirrorPlugins() {
return [
new Plugin({
view: () => ({
update() {
new ReactRenderer(Component, ...) // calls flushSync inside the constructor
}
})
})
]
}
})
function Editor() {
const [, setState] = useState(1)
const editor = useEditor({...}, [])
function onAction() {
setState(2)
editor.commands.setTextSelection(0) // triggers plugin view update
}
return (...)
}
Calling onAction will result in flushSync was called from inside a lifecycle method. error.
Event chain:
- plugin view update is triggered
ReactRenderercallsflushSyncEditorcomponent gets re-rendered because of the new stateuseEditorcallseditor.setOptionswhich again triggers the plugin view update
I believe editor options should be updated only when they change.
Browser Used
Chrome
Code Example URL
No response
Expected Behavior
No error is reported.
Additional Context (Optional)
No response
Dependency Updates
- Yes, I’ve updated all my dependencies.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 25 (25 by maintainers)
Commits related to this issue
- perf(react): do some light diffing to not reset options on every render #6024 — committed to ueberdosis/tiptap by nperez0111 2 years ago
- perf(react): do some light diffing to not reset options on every render #6024 (#6031) This does a shallow diff between the current options and the incoming ones to determine whether we should try to ... — committed to ueberdosis/tiptap by nperez0111 2 years ago
@yurtsiv, would you mind validating the fix with this version please?
From here: https://github.com/ueberdosis/tiptap/pull/6031#issuecomment-2598876644
I fixed this issue more out of the potential perf benefits rather than actually endorsing this sort of usage.
Released in version 2.11.3
The patch is working 🎉 Had to memoize
editorPropshowever. Perhaps worth mentioning in the documentation (same forparseOptions,coreExtensionOptions, and other complex options)controlled component pattern is very harmful. React has trouble getting those to run even with basic input boxes, much less an entire rich text editor. I would very much advise you against it unless you want to solve performance issues later on.
It is much more performant to get the value from the editor instance when you actually need it rather than trying to synchronize it with some state. Like this:
DO NOT DO THIS:
DO THIS:
We will see how it goes, https://github.com/ueberdosis/tiptap/pull/6031
A simple workaround for now is to avoid that code path by adding dummy deps