Skip to content

Instantly share code, notes, and snippets.

@isNan909
Created August 2, 2022 06:34
Show Gist options
  • Select an option

  • Save isNan909/ec5b5ea291f3866088a03994bcfe0f56 to your computer and use it in GitHub Desktop.

Select an option

Save isNan909/ec5b5ea291f3866088a03994bcfe0f56 to your computer and use it in GitHub Desktop.
useWindowSize hook
const useWindowSize = () => {
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined
});
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight
});
}
window.addEventListener("resize", handleResize);
handleResize();
return () => window.removeEventListener("resize", handleResize);
}, []);
return windowSize;
};
//const windowSize = useWindowSize(); use it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment