psc.logos.co/hooks/useWindowSize.ts

30 lines
572 B
TypeScript
Raw Normal View History

2024-08-19 16:23:24 +00:00
import { useEffect, useState } from 'react'
function useWindowSize() {
const [windowSize, setWindowSize] = useState({
width: 0,
height: 0,
})
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
})
}
window.addEventListener('resize', handleResize)
handleResize()
return () => window.removeEventListener('resize', handleResize)
}, [])
const { width, height } = windowSize
return {
width,
height,
}
}
export default useWindowSize