Skip to content

Instantly share code, notes, and snippets.

@tjinlag
Created April 9, 2023 12:21
Show Gist options
  • Save tjinlag/2ea1d783068b0dfa69b7ca49b4b62390 to your computer and use it in GitHub Desktop.
Save tjinlag/2ea1d783068b0dfa69b7ca49b4b62390 to your computer and use it in GitHub Desktop.
React Custom Hook: useWindowSize
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