Created
July 14, 2021 23:00
-
-
Save marcovincit/6dfaabbd1938571f4cad4934e534b565 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 { 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