Skip to content

Commit 3ad4a74

Browse files
committed
Use effect for hint scroll listener
1 parent 31b1802 commit 3ad4a74

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/components/Hint/Hint.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,23 @@ function copyStyles(inputNode: HTMLInputElement, hintNode: HTMLInputElement) {
3939

4040
export const useHint = () => {
4141
const { hintText, inputNode } = useTypeaheadContext();
42-
42+
4343
const hintRef = useRef<HTMLInputElement | null>(null);
4444

45-
// scroll hint input when the text input is scrolling
46-
inputNode?.addEventListener("scroll", () => {
47-
hintRef.current!.scrollLeft = inputNode?.scrollLeft
48-
})
49-
45+
useEffect(() => {
46+
// Scroll hint input when the text input is scrolling.
47+
const handleInputScroll = () => {
48+
if (hintRef.current && inputNode) {
49+
hintRef.current.scrollLeft = inputNode.scrollLeft;
50+
}
51+
};
52+
53+
inputNode?.addEventListener('scroll', handleInputScroll);
54+
return () => {
55+
inputNode?.removeEventListener('scroll', handleInputScroll);
56+
};
57+
}, [inputNode]);
58+
5059
useEffect(() => {
5160
if (inputNode && hintRef.current) {
5261
copyStyles(inputNode, hintRef.current);

0 commit comments

Comments
 (0)