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
barry
barry17mo ago
You need to pass the same function Make the handler a function and pass it both places Also dont use images for code
adomaitis
adomaitis17mo ago
Yeah that is not working also
barry
barry17mo ago
Show
adomaitis
adomaitis17mo ago
function printNumberFive() { console.log(5); } useEffect(() => { scrollRef.current!.addEventListener("scroll", () => { printNumberFive(); }); return () => { scrollRef.current!.removeEventListener("scroll", () => { printNumberFive(); }); }; }, [selectedItem]);
barry
barry17mo ago
Still 2 different references Just pass printNumberFive
adomaitis
adomaitis17mo ago
do you mean on the cleanup?
barry
barry17mo ago
both
adomaitis
adomaitis17mo ago
oh shit trying it cool thx it worked
Want results from more Discord servers?
Add your server
More Posts