Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { useState, useEffect } from "react";
export default function useWindowSize() {
const [{width, height}, setSize] = useState({ window.innerWidth, window.innerHeight });
useEffect(() => {
const handleResize = () => setSize({ window.innerWidth, window.innerHeight });
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
};
}, [setSize]);
return {width, height}; // or: return [width, height]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment