blog / hooks /useWindowSize.ts
sandy-try's picture
Upload 699 files
1b72d7e verified
raw
history blame contribute delete
672 Bytes
import { useEffect, useState } from 'react'
interface WindowSize {
width: number,
height: number
}
const useWindowSize = () => {
const [size, setSize] = useState<WindowSize>({
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
})
useEffect(() => {
const onResize = () => {
setSize({
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
})
}
onResize()
window.addEventListener('resize', onResize)
return () => {
window.removeEventListener('resize', onResize)
}
}, [])
return size
}
export default useWindowSize