Hi, this a nice way to keep track of the screen size though I would add it's important to remove event listeners when you detach the hook. You'd do that by having a return in your useEffect.
import { useEffect, useState } from "react"; export useWindowWidth() { const [width, setWidth] = useState(window.screen.width); useEffect(() => { const updateWidth = () => setWidth(window.screen.width); window.addEventListener('resize', updateWidth); return () => window.removeEventListener('resize', updateWidth); },[]); return width; }
Also it might be worth looking at matchMedia as that will allow you to do more things.
Thanks for pointing that out, quite an important one, left that my bad
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi, this a nice way to keep track of the screen size though I would add it's important to remove event listeners when you detach the hook. You'd do that by having a return in your useEffect.
Also it might be worth looking at matchMedia as that will allow you to do more things.
Thanks for pointing that out, quite an important one, left that my bad