From 0da76cbfec8341a9dd33a9288e25c4779967d274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20=C5=BB=C3=B3=C5=82kiewski?= Date: Tue, 30 Jun 2026 14:32:00 +0200 Subject: [PATCH] fix(web): create custom hook for useRef logic --- src/web/EnrichedTextInput.tsx | 65 ++++++++++------------------------- src/web/useStableRef.ts | 12 +++++++ 2 files changed, 30 insertions(+), 47 deletions(-) create mode 100644 src/web/useStableRef.ts diff --git a/src/web/EnrichedTextInput.tsx b/src/web/EnrichedTextInput.tsx index 3f7e35281..59781a141 100644 --- a/src/web/EnrichedTextInput.tsx +++ b/src/web/EnrichedTextInput.tsx @@ -76,6 +76,7 @@ import { StripMarksOnImagePlugin } from './pmPlugins/StripMarksOnImagePlugin'; import { ShortcutPlugin } from './pmPlugins/ShortcutPlugin'; import { returnKeyTypeToEnterKeyHint } from './returnKeyTypeToEnterKeyHint'; import { AutolinkPlugin } from './pmPlugins/AutolinkPlugin'; +import { useStableRef } from './useStableRef'; function runFocused( editor: Editor, @@ -126,56 +127,26 @@ export const EnrichedTextInput = ({ () => mergeWithDefaultHtmlStyle(htmlStyle), [htmlStyle] ); - - const htmlStyleRef = useRef(resolvedHtmlStyle); - useEffect(() => { - htmlStyleRef.current = resolvedHtmlStyle; - }, [resolvedHtmlStyle]); - - const onPasteImagesRef = useRef(onPasteImages); - useEffect(() => { - onPasteImagesRef.current = onPasteImages; - }, [onPasteImages]); - - const mentionIndicatorsRef = useRef(mentionIndicators); - useEffect(() => { - mentionIndicatorsRef.current = mentionIndicators; - }, [mentionIndicators]); - - const mentionCallbacksRef = useRef({ - onStartMention, - onChangeMention, - onEndMention, - onMentionDetected, - }); - useEffect(() => { - mentionCallbacksRef.current = { + const mentionCallbacks = useMemo( + () => ({ onStartMention, onChangeMention, onEndMention, onMentionDetected, - }; - }, [onStartMention, onChangeMention, onEndMention, onMentionDetected]); - - const submitBehaviorRef = useRef(submitBehavior); - const onSubmitEditingRef = useRef(onSubmitEditing); - const onKeyPressRef = useRef(onKeyPress); - const editorInstanceRef = useRef(null); + }), + [onStartMention, onChangeMention, onEndMention, onMentionDetected] + ); - useEffect(() => { - submitBehaviorRef.current = submitBehavior; - }, [submitBehavior]); - useEffect(() => { - onSubmitEditingRef.current = onSubmitEditing; - }, [onSubmitEditing]); - useEffect(() => { - onKeyPressRef.current = onKeyPress; - }, [onKeyPress]); + const htmlStyleRef = useStableRef(resolvedHtmlStyle); + const onPasteImagesRef = useStableRef(onPasteImages); + const mentionIndicatorsRef = useStableRef(mentionIndicators); + const submitBehaviorRef = useStableRef(submitBehavior); + const onSubmitEditingRef = useStableRef(onSubmitEditing); + const onKeyPressRef = useStableRef(onKeyPress); + const useHtmlNormalizerRef = useStableRef(useHtmlNormalizer); + const mentionCallbacksRef = useStableRef(mentionCallbacks); - const useHtmlNormalizerRef = useRef(useHtmlNormalizer); - useEffect(() => { - useHtmlNormalizerRef.current = useHtmlNormalizer; - }, [useHtmlNormalizer]); + const editorInstanceRef = useRef(null); const handleKeyDown = (doc: Node, event: KeyboardEvent): boolean => { onKeyPressRef.current?.(adaptWebToNativeEvent(event, { key: event.key })); @@ -252,7 +223,7 @@ export const EnrichedTextInput = ({ showOnlyWhenEditable: true, }), ], - [placeholder] + [placeholder, htmlStyleRef, mentionIndicatorsRef] ); const editor = useEditor( @@ -324,7 +295,7 @@ export const EnrichedTextInput = ({ useEffect(() => { if (!editor) return; return subscribeMentionEvents(editor, () => mentionCallbacksRef.current); - }, [editor]); + }, [editor, mentionCallbacksRef]); useOnChangeHtml(editor, onChangeHtml); useOnChangeText(editor, onChangeText); @@ -388,7 +359,7 @@ export const EnrichedTextInput = ({ setNativeProps: () => {}, setTextAlignment: () => {}, }), - [editor] + [editor, mentionIndicatorsRef, useHtmlNormalizerRef] ); const editorStyle: CSSProperties = useMemo( diff --git a/src/web/useStableRef.ts b/src/web/useStableRef.ts new file mode 100644 index 000000000..7725944dc --- /dev/null +++ b/src/web/useStableRef.ts @@ -0,0 +1,12 @@ +import { useEffect, useRef, type RefObject } from 'react'; + +//TODO: When upgrading to React 19.2 migrate to useEffectEvent instead +export const useStableRef = (value: T): RefObject => { + const ref = useRef(value); + + useEffect(() => { + ref.current = value; + }, [value]); + + return ref; +};