How to implement a "scroll detection" feature using React? I would like to present the user with a div with a given message, and enable a checkbox only once the user has scrolled through it. This is my current implementation, which is not working.
function Disclaimer({ text, checkboxText }) {
const divRef = useRef();
const [userHasReviewed, setUserHasReviewed] = useState(false);
const scrollTracker = () => {
if (divRef.current) {
const { scrollTop, scrollHeight, clientHeight } =
divRef.current;
if (scrollTop + clientHeight === scrollHeight) {
setUserHasReviewed(true);
}
}
};
return (
<div>
<div
ref={divRef}
onScroll={() => scrollTracker()}
>
{text}
</div>
<div>
<Checkbox
label={checkboxText}
disabled={!userHasReviewed}
/>
</div>
</div>
);
}
I've placed debug points inside of the scrollTracker function, but the debugger never enters the function. I think this is because the text is not large enough to actually overflow and require the scrolling space/action. In this case, how can I automatically detect this so that the userHasReviewed is set to true automatically?