Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KitsonBroadhurst/59ca6a68620b1a2b15c8f833dee42ce8 to your computer and use it in GitHub Desktop.
Save KitsonBroadhurst/59ca6a68620b1a2b15c8f833dee42ce8 to your computer and use it in GitHub Desktop.
useWindowSize custom hook
import React, { useState, useLayoutEffect } from 'react'
const useWindowSize = () => {
const [size, setSize] = useState([0, 0])
useLayoutEffect(() => {
const updateSize = () => {
setSize([window.innerWidth, window.innerHeight])
}
window.addEventListener('resize', updateSize)
updateSize()
return () => window.removeEventListener('resize', updateSize)
}, [])
return size
}
export default useWindowSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment