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
  • ReactRenderer calls flushSync
  • Editor component gets re-rendered because of the new state
  • useEditor calls editor.setOptions which 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

Most upvoted comments

@yurtsiv, would you mind validating the fix with this version please?

npm i https://pkg.pr.new/@tiptap/react@6031

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 editorProps however. Perhaps worth mentioning in the documentation (same for parseOptions, 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:

function Component() {
  const [editorContent, setEditorContent] = React.useState(`
        <p>This isn’t bold.</p>
        <p><strong>This is bold.</strong></p>
      `)
  const editor = useEditor({
    extensions: [Document, Paragraph, Text, Bold],
    content: editorContent,
    onUpdate: ({ editor }) => {
      setEditorContent(editor.getHTML())
    },
  })

  return (
    <>
      {editorContent}
      <button onClick={() => saveToDB(editorContent)}>Send somewhere</button>
    </>
  )
}

DO THIS:


function Component() {
  const editor = useEditor({
    extensions: [Document, Paragraph, Text, Bold],
    content: editorContent,
    content: `
        <p>This isn’t bold.</p>
        <p><strong>This is bold.</strong></p>
      `,
  })

  return (
    <>
      {editor.getHTML()}
      <button onClick={() => saveToDB(editor.getHTML())}>Send somewhere</button>
    </>
  )
}

A simple workaround for now is to avoid that code path by adding dummy deps

const editor = useEditor({...}, [1])