Created
April 9, 2023 12:21
-
-
Save tjinlag/2ea1d783068b0dfa69b7ca49b4b62390 to your computer and use it in GitHub Desktop.
React Custom Hook: useWindowSize
This file contains 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 } from "react" | |
import useEventListener from "./useEventListener" | |
export default function useWindowSize() { | |
const [windowSize, setWindowSize] = useState({ | |
width: window.innerWidth, | |
height: window.innerHeight, | |
}) | |
useEventListener("resize", () => { | |
setWindowSize({ width: window.innerWidth, height: window.innerHeight }) | |
}) | |
return windowSize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment