Created
November 7, 2019 15:07
-
-
Save Siemko/b31412fdfb9a1a9b8bf38e6a02d504eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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