Skip to content

Instantly share code, notes, and snippets.

@marcovincit
Created July 14, 2021 23:00
Show Gist options
  • Save marcovincit/6dfaabbd1938571f4cad4934e534b565 to your computer and use it in GitHub Desktop.
Save marcovincit/6dfaabbd1938571f4cad4934e534b565 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
export default function useWindowSize() {
const [windowWidth, setWindowWidth] = useState(0);
const [windowHeight, setWindowHeight] = useState(0);
useEffect(() => {
function updateSize() {
setWindowWidth(window.innerWidth);
setWindowHeight(window.innerHeight);
}
window.addEventListener("resize", updateSize);
updateSize();
return () => window.removeEventListener("resize", updateSize);
}, []);
return { windowWidth, windowHeight };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment