diff --git a/src/components/Hint/Hint.tsx b/src/components/Hint/Hint.tsx index 503e2122..bc8a1b61 100644 --- a/src/components/Hint/Hint.tsx +++ b/src/components/Hint/Hint.tsx @@ -39,14 +39,23 @@ function copyStyles(inputNode: HTMLInputElement, hintNode: HTMLInputElement) { export const useHint = () => { const { hintText, inputNode } = useTypeaheadContext(); - + const hintRef = useRef(null); - // scroll hint input when the text input is scrolling - inputNode?.addEventListener("scroll", () => { - hintRef.current!.scrollLeft = inputNode?.scrollLeft - }) - + useEffect(() => { + // Scroll hint input when the text input is scrolling. + const handleInputScroll = () => { + if (hintRef.current && inputNode) { + hintRef.current.scrollLeft = inputNode.scrollLeft; + } + }; + + inputNode?.addEventListener('scroll', handleInputScroll); + return () => { + inputNode?.removeEventListener('scroll', handleInputScroll); + }; + }, [inputNode]); + useEffect(() => { if (inputNode && hintRef.current) { copyStyles(inputNode, hintRef.current);