import {useLayoutEffect, useState} from 'react'
export default function useWindowSize() {
const [size, setSize] = useState([0, 0])
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight])
}
window.addEventListener('resize', updateSize)
updateSize()
return () => window.removeEventListener('resize', updateSize)
}, [])
return size
}
import {useLayoutEffect, useState} from 'react'
export default function useWindowSize() {
const [windowSize, setWindowSize] = useState()
useLayoutEffect(() => {
if (!windowSize) setWindowSize([window.innerWidth, window.innerHeight])
let resizeTimer
const windowSizer = () => {
clearTimeout(resizeTimer)
resizeTimer = setTimeout(() => {
setWindowSize([window.innerWidth, window.innerHeight])
}, 300)
}
window.addEventListener('resize', windowSizer)
return () => {
window.removeEventListener('resize', windowSizer)
}
}, [windowSize])
return windowSize
}