From 288bf4299a3a0bdff0cc4c9d25b4eeea77060acc Mon Sep 17 00:00:00 2001 From: Vlad Riabchenko Date: Mon, 6 Nov 2023 14:12:40 +0100 Subject: [PATCH] [FIX] ref remains null --- lib/smooth-scrollbar.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/smooth-scrollbar.ts b/lib/smooth-scrollbar.ts index 6806780..ad8d5aa 100644 --- a/lib/smooth-scrollbar.ts +++ b/lib/smooth-scrollbar.ts @@ -46,9 +46,6 @@ const SmoothScrollbarReact = forwardRef( }, []); useEffect(() => { - if (ref) { - (ref as React.MutableRefObject).current = scrollbar.current; - } return () => { if (scrollbar.current) { scrollbar.current.removeListener(handleScroll); @@ -57,6 +54,12 @@ const SmoothScrollbarReact = forwardRef( }; }, []); + useEffect(() => { + if (ref) { + (ref as React.MutableRefObject).current = scrollbar.current; + } + }, [scrollbar.current]); + useEffect(() => { if (mountedRef.current === true) { if (scrollbar.current) {