DEV Community

Discussion on: The React Cheatsheet for 2020 📄 (+ Real-World Examples)

Collapse
 
dvdvdmt profile image
Dmitriy Davydov

Hi Reed, thank you for the cheatsheet. I found an error in Mouse Tracker example.
This code will not remove mousemove handler:

window.removeEventListener("mousemove", event => {
        const { pageX, pageY } = event;
        setMousePosition({ x: pageX, y: pageY });
      });

Here is a life example. After you press 'Toggle mouse tracker' you'll see an error in console. It happens because you should provide a reference to the handler that you want unsubscribe from. So here is the valid example.

Collapse
 
reedbarger profile image
Reed Barger

You're right, we need a reference to the same function. That was my original code, but I removed the reference mistakenly because I thought the abstraction of the callback to handleMouseMove was unneeded. It's fixed now. Thanks Dmitriy!

Collapse
 
tangobravo profile image
tangobravo • Edited

There's still a comment at the end of that example that dates from the original version too.