Skip to content
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

Fix React Dependency Array creating new editor instances instead of reusing existing one #4453

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
50 changes: 50 additions & 0 deletions demos/src/Issues/2403/React/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import './styles.scss'

import Document from '@tiptap/extension-document'
import Heading from '@tiptap/extension-heading'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React, { useState } from 'react'

const TiptapEditor = ({ count, editorClass }) => {
const editor = useEditor({
extensions: [
Document,
Heading,
Paragraph,
Text,
],
editorProps: {
attributes: {
class: editorClass,
},
},
onUpdate: () => {
console.log(count)
},
}, [count, editorClass])

if (!editor) {
return null
}

return (
<div>
<EditorContent editor={editor} />
</div>
)
}

export default () => {
const [count, setCount] = useState(0)
const [editorClass, setEditorClass] = useState('my-editor')

return (
<>
<button onClick={() => setCount(count + 1)}>Inc</button>
<button onClick={() => setEditorClass('my-editor updated-editor')}>Update class</button>
<TiptapEditor editorClass={editorClass} count={count} />
</>
)
}
6 changes: 6 additions & 0 deletions demos/src/Issues/2403/React/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Basic editor styles */
.tiptap {
> * + * {
margin-top: 0.75em;
}
}
25 changes: 16 additions & 9 deletions packages/react/src/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,31 @@ export const useEditor = (options: Partial<EditorOptions> = {}, deps: Dependency
useEffect(() => {
let isMounted = true

const instance = new Editor(options)
// if the editor does not exist yet, create a new
// editor instance
// if the editor does exist, update the editor options accordingly
if (!editor) {
const instance = new Editor(options)

setEditor(instance)
setEditor(instance)

instance.on('transaction', () => {
requestAnimationFrame(() => {
instance.on('transaction', () => {
requestAnimationFrame(() => {
if (isMounted) {
forceUpdate()
}
requestAnimationFrame(() => {
if (isMounted) {
forceUpdate()
}
})
})
})
})
} else {
editor.setOptions(options)
}

return () => {
isMounted = false
}
}, deps)
}, [deps])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line wrongly double-wraps the dependencies in an additional array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - fixed


useEffect(() => {
return () => {
Expand Down