I’m trying to stop listening to the scroll event because I need to control when the event should scroll and when to remove the scroll event.
I’m trying to e.prevent but it doesnot work. Is there any way to controll the scroll by using react-js?
const handleScroll = (e) => {
//I need to stop scrolling when a certain condition is true
if (e.target.scrollHeight == 0) {
// e.removeEventListener('scroll', handleScroll); DOES NOT WORK
//e.pree.preventdefault() DOES NOT WORK
} else {
//Turn on the listenner
}
}
return (
<main onScroll={handleScroll}data-status={valor}>
<div>Conteudo</div>
</main >
);
If you guys can help me i really appreciate it. Thanks for all, Have a good day.
2
Answers
Try with this Code :
Here’s how you might implement this:
In your CSS (App.css):
This method uses a state variable to track whether scrolling should be enabled and applies a CSS class to control the actual scrolling. It’s a more React-centric way of handling this issue, fitting well with the declarative nature of React. Additionally, it’s a good practice to clean up event listeners when the component unmounts to avoid memory leaks and unexpected behavior, which is handled in the useEffect cleanup function.