Skip to content

Instantly share code, notes, and snippets.

@SitaGomes
Created January 10, 2024 16:53
Show Gist options
  • Save SitaGomes/de1a86611e8f2fba7ce698fbeb4e18e7 to your computer and use it in GitHub Desktop.
Save SitaGomes/de1a86611e8f2fba7ce698fbeb4e18e7 to your computer and use it in GitHub Desktop.
A hook that returns the current view port of the page
export const useGetViewPort = () => {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
const handleWindowResize = () => setWidth(window.innerWidth);
window.addEventListener("resize", handleWindowResize);
return () => window.removeEventListener("resize", handleWindowResize);
}, []);
// return the width so we can use it in our components
return width;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment