useEffect being weir - EventListener
So... I have this listener that listens for the position of selectedItem when I scroll on its parent. Why is it not unbinding when selectedItem changes? the cleanup function doesn't work. I have no idea of what is wrong lol.
when I change the selectedItem, it creates a new listener for that and they keep stacking.
please send help. images attached.
8 Replies
You need to pass the same function
Make the handler a function and pass it both places
Also dont use images for code
Yeah that is not working also
Show
function printNumberFive() {
console.log(5);
}
useEffect(() => {
scrollRef.current!.addEventListener("scroll", () => {
printNumberFive();
});
return () => {
scrollRef.current!.removeEventListener("scroll", () => {
printNumberFive();
});
};
}, [selectedItem]);
Still 2 different references
Just pass printNumberFive
do you mean on the cleanup?
both
oh shit
trying it
cool
thx
it worked