chore: memoize the editor context value

This commit is contained in:
Nick the Sick 2024-10-28 16:03:31 +01:00
parent 6b2df67219
commit e2787e4736
No known key found for this signature in database
GPG Key ID: F575992F156E5BCC

View File

@ -1,6 +1,7 @@
import { Editor } from '@tiptap/core' import { Editor } from '@tiptap/core'
import React, { import React, {
createContext, HTMLAttributes, ReactNode, useContext, createContext, HTMLAttributes, ReactNode, useContext,
useMemo,
} from 'react' } from 'react'
import { EditorContent } from './EditorContent.js' import { EditorContent } from './EditorContent.js'
@ -37,13 +38,14 @@ export function EditorProvider({
children, slotAfter, slotBefore, editorContainerProps = {}, ...editorOptions children, slotAfter, slotBefore, editorContainerProps = {}, ...editorOptions
}: EditorProviderProps) { }: EditorProviderProps) {
const editor = useEditor(editorOptions) const editor = useEditor(editorOptions)
const editorContextValue = useMemo(() => ({ editor }), [editor])
if (!editor) { if (!editor) {
return null return null
} }
return ( return (
<EditorContext.Provider value={{ editor }}> <EditorContext.Provider value={editorContextValue}>
{slotBefore} {slotBefore}
<EditorConsumer> <EditorConsumer>
{({ editor: currentEditor }) => ( {({ editor: currentEditor }) => (