feat: create custom hook for window size

This commit is contained in:
RadoslavDimchev 2024-01-18 21:55:50 +02:00
parent 0c610343d1
commit 5ca1529dc1

View File

@ -0,0 +1,22 @@
import { useState, useEffect } from 'react'
export const useWindowSize = () => {
const [size, setSize] = useState({ width: 0, height: 0 })
useEffect(() => {
function handleResize() {
setSize({
width: window.innerWidth,
height: window.innerHeight,
})
}
window.addEventListener('resize', handleResize)
handleResize()
return () => window.removeEventListener('resize', handleResize)
}, [])
return size
}