JakubHJ
Reactiflux4y ago
1 reply
JakubH

venus – 14-11 Aug 22

Hello. I want to set state on true when is user scrolled on top/bottom, but I want to avoid re-rendering whole component on each scroll. What would you recommend me to do?

const [isReached, setIsReached] = useState({ top: false, bottom: false });

<div
  onScroll={(e: any) => {
    const top = e.target.scrollTop === 0;
    const bottom =
      e.target.scrollHeight - e.target.scrollTop ===
      e.target.clientHeight;

    setIsReached({ bottom: bottom, top: top });
  }}
/>
Was this page helpful?