diff --git a/packages/smarthr-ui/src/components/Table/useReelCells.ts b/packages/smarthr-ui/src/components/Table/useReelCells.ts index 5927946b8d..812a9435b8 100644 --- a/packages/smarthr-ui/src/components/Table/useReelCells.ts +++ b/packages/smarthr-ui/src/components/Table/useReelCells.ts @@ -7,42 +7,41 @@ export const useReelCells = () => { useEffect(() => { const currentRef = tableWrapperRef.current + if (!currentRef) { + return () => undefined + } + const handleScroll = () => { - if (currentRef) { - const stickyCells = currentRef.querySelectorAll('.fixedElement') || [] - const scrollLeft = currentRef.scrollLeft - const maxScrollLeft = currentRef.scrollWidth - currentRef.clientWidth || 0 - - stickyCells.forEach((cell) => { - const shouldFix = maxScrollLeft > 0 && scrollLeft < maxScrollLeft - - if (shouldFix) { - cell.classList.add('fixed') - setShowShadow(scrollLeft > 0) - } else { - cell.classList.remove('fixed') - setShowShadow(maxScrollLeft === 0 && scrollLeft === 0 ? false : true) - } - }) - } + const stickyCells = currentRef.querySelectorAll('.fixedElement') || [] + const scrollLeft = currentRef.scrollLeft + const maxScrollLeft = currentRef.scrollWidth - currentRef.clientWidth || 0 + + stickyCells.forEach((cell) => { + const shouldFix = maxScrollLeft > 0 && scrollLeft < maxScrollLeft + + if (shouldFix) { + cell.classList.add('fixed') + setShowShadow(scrollLeft > 0) + } else { + cell.classList.remove('fixed') + setShowShadow(maxScrollLeft === 0 && scrollLeft === 0 ? false : true) + } + }) } + handleScroll() - currentRef?.addEventListener('scroll', handleScroll) + currentRef.addEventListener('scroll', handleScroll) const observer = new window.ResizeObserver(() => { handleScroll() }) - if (currentRef) { - observer.observe(currentRef) - } + observer.observe(currentRef) return () => { - if (currentRef) { - currentRef.removeEventListener('scroll', handleScroll) - observer.unobserve(currentRef) - } + currentRef.removeEventListener('scroll', handleScroll) + observer.unobserve(currentRef) } }, [tableWrapperRef, setShowShadow])