tiptap icon indicating copy to clipboard operation
tiptap copied to clipboard

[Bug]: useEditor always creates at least 2 editors on mount

Open schontz opened this issue 7 months ago • 5 comments

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, before deps has actually changed. If deps are supplied, then the editor created in useState will get destroyed immediately in useEffect and get re-created.

It may be generally useful to have a hook that only runs when things change, e.g.:

function useChangeEffect(effect, deps) {
  const isFirstTime = useRef(true);
  useEffect(() => {
    if (isFirstTime.current) isFirstTime.current = false;
    else return effect();
  }, deps);
}

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 when options change. That means updated options 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

  • [X] Yes, I've updated all my dependencies.

schontz avatar Aug 01 '24 14:08 schontz