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
barry2y 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
adomaitisOP2y ago
Yeah that is not working also
barry
barry2y ago
Show
adomaitis
adomaitisOP2y ago
function printNumberFive() { console.log(5); } useEffect(() => { scrollRef.current!.addEventListener("scroll", () => { printNumberFive(); }); return () => { scrollRef.current!.removeEventListener("scroll", () => { printNumberFive(); }); }; }, [selectedItem]);
barry
barry2y ago
Still 2 different references Just pass printNumberFive
adomaitis
adomaitisOP2y ago
do you mean on the cleanup?
barry
barry2y ago
both
adomaitis
adomaitisOP2y ago
oh shit trying it cool thx it worked
Want results from more Discord servers?
Add your server