Skip to content
Advertisement

Why does addEventListener and removeEventListener inside useEffect() require an arrow function?

I’m trying to set onTop state to true if the user has scrolled to the top and false otherwise. I tried the following.

JavaScript

The above example doesn’t work but throws no error either.

JavaScript

Note that I added an arrow and braces to the second parameter function. The above example works as intended. Can anyone explain why? Thanks very much!

Advertisement

Answer

The reason your initial function doesn’t work well is because on every re-render a new instance of the function is created and thee previous one is removed since you are passing the function as a dependency to useEffect. Also since you are not executing the window.removeEventListener in a cleanup function, it runs immediately causing the listener to be removed immediately.

You can solve it either by

JavaScript

or

JavaScript

Also note that with arrow functions, you solution works because to removeEventListener you need to pass the same function reference for it to work properly, if you use arrow function, the listener doesn’t cleanup up and hence your implementation works

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement