DEV Community

Discussion on: Navbar hide and show on Scroll using Custom React Hooks

Collapse
 
hasnaindev profile image
Muhammad Hasnain

Ooh yes! Adding event listeners like that is a really really good example of a memory leak. I once read an article where soundcloud developers said that their app was getting slower after a lot of use. It was because they had the same issue. Attaching events without removing them.

Also, the Sandbox you shared, there is an empty folder called useScrollListener. The import names are a little confusing too. I also want to know why you didn't use a useRef hook instead of using the className attribute?

Collapse
 
link2twenty profile image
Andrew Bone

I've fixed the names, I changed the name part way through making the demo so I guess it got confused and kept made a new version rather than renaming.

Controlling the classes like this means you know for sure what classes you element has if you start adding and removing them with JS you have to keep track of all the classes and make sure you don't remove any by accident, or even leave any on the element when you don't mean to.