Skip to content

Instantly share code, notes, and snippets.

@klippx
Created November 12, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klippx/369b3d50f9ca89051564b299eb08f10f to your computer and use it in GitHub Desktop.
Save klippx/369b3d50f9ca89051564b299eb08f10f to your computer and use it in GitHub Desktop.
useWindowSize [web]
import { useEffect, useState } from 'react'
const useWindowSize = () => {
const { innerWidth: width, innerHeight: height } = window
const [state, setState] = useState({ width, height })
useEffect(() => {
const handler = () => {
setState({
width: window.innerWidth,
height: window.innerHeight,
})
}
window.addEventListener('resize', handler)
return () => window.removeEventListener('resize', handler)
}, [])
return state
}
export default useWindowSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment