-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: useEditor always creates at least 2 editors on mount #5432
Comments
Thanks for bringing it up, I was a bit too focused on another bug. I'll consider your some of your suggestions. |
Looking at
I was working on a PR for this, but there are multiple issues with the logic and I worry a little about pulling the thread. I'm not clear under what conditions useEffect cleanup will run before the editor has been replaced, since it will be run after deps change (which necessitates a new editor). The change that introduced this was supposedly for a strict mode issue, but because we create the editor via That said, it is more react-y if it is not available on sync and instead runs all the commands via a useEffect. For example this bug wants to focus the editor right away, but in React the DOM isn't available until effects run, so the prescribed method should be something like: const editor = useEditor(...)
useEffect(() => {
if (!editor) return;
editor.commands.focus();
}, [editor]);
// or via onCreate
const editor = useEditor({
onCreate: ({ editor }) => editor.commands.focus()
}) Although there are scenarios when the editor gets re-created, which would call that effect multiple times. This could be solved via an option, though a properly descriptive name is hard to come by: const editor = useEditor({
onFirstTimeCreate: ({ editor }) => editor.commands.focus()
}) There could be more discussion around the API as a whole. For example,
I know that doesn't play well with I put together a little StackBlitz Sample that shows some of these issues with a fake editor in strict and non strict modes. You can see that in both modes, the timeout destroy is not running on dismount. |
Note that I am able to apply some of the patches locally and it stops creating new editors, though we'll have to refactor the "useState for new editor" if we really want things to be safe in React land. |
…r of instances created while being performant #5432
Here is my stab at it: #5445 I think the crux of the change was to have the effect run on every render (without a dep array) and to schedule destruction of instances, but bail on the actual destruction if the instance was still mounted and nothing else changed |
Cross-posting here for visibility: #5414 (comment) |
@nperez0111 Did this issue not get fixed with #5445? Is there a reason why the issue remains open? |
Working on releasing it now actually 😅 My CI has a bug that kept trying to release it as a minor that I was fighting. Will get it out as a patch |
This is now released with v2.5.9! |
Affected Packages
react
Version(s)
develop
Bug Description
The editor is destroyed and re-created whenever deps changes. But this
useEffect
will run once on mount to start, beforedeps
has actually changed. Ifdeps
are supplied, then the editor created inuseState
will get destroyed immediately inuseEffect
and get re-created.It may be generally useful to have a hook that only runs when things change, e.g.:
Then you can just replace this useEffect with
useChangeEffect
.On a related note, applying options instead of re-creating an editor is useless code, because the effect only runs when
deps
change, not whenoptions
change. That means updatedoptions
are never injected into the editor. Furthermore, mostRecentOptions is a misnomer, because it is not actually updated on each render, so it is effectively useless.Browser Used
Chrome
Code Example URL
No response
Expected Behavior
Editor is only created once if deps do not change.
Additional Context (Optional)
No response
Dependency Updates
The text was updated successfully, but these errors were encountered: