Skip to content

Getting access to the editor from outside the React component? #2223

Discussion options

You must be logged in to vote

Apparently useEditor returns null at first and later it doesn't.

This is how I fixed it:

export type TiptapMethods = {
  clearContent: () => void
}

let Tiptap = forwardRef((props: { content: string }, ref: React.ForwardedRef<TiptapMethods>) => {
  const editor = useEditor({
    extensions,
    content: props.content,
  })
  const editorRef: React.MutableRefObject<Editor | null> = useRef(null)
  useImperativeHandle(ref, () => ({
    clearContent: () => {
      console.log('clearContent')
      console.log(editorRef)
      editorRef.current?.commands.clearContent()
    }
  }))
  if (!editor) return null
  editorRef.current = editor
  return (
    <EditorContent editor={editor} />
  )
})
Ti…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@philippkuehn
Comment options

Answer selected by neongreen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants