Files
tiptap/packages/react/src/useEditor.ts
Yousef bdd6e2b87c Allow passing of DependencyList to useEditor
in case the options depend on props of the consuming component, it would be nice to be able to pass a dependencylist to `useEditor`. Unless there's a better way to handle dependency updates
2021-05-25 14:51:36 +02:00

29 lines
659 B
TypeScript

import { useState, useEffect, DependencyList } from 'react'
import { EditorOptions } from '@tiptap/core'
import { Editor } from './Editor'
function useForceUpdate() {
const [, setValue] = useState(0)
return () => setValue(value => value + 1)
}
export const useEditor = (options: Partial<EditorOptions> = {}, deps: DependencyList = []) => {
const [editor, setEditor] = useState<Editor | null>(null)
const forceUpdate = useForceUpdate()
useEffect(() => {
const instance = new Editor(options)
setEditor(instance)
instance.on('transaction', forceUpdate)
return () => {
instance.destroy()
}
}, deps)
return editor
}