Created
August 23, 2023 01:19
-
-
Save prmichaelsen/bacdf37d1a60e615cd0dddcf29d61a93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect, useCallback } from 'react'; | |
const getSize = () => { | |
return { | |
width: window.innerWidth, | |
height: window.innerHeight, | |
}; | |
}; | |
export function useResize() { | |
const [size, setSize] = useState(getSize()); | |
const handleResize = useCallback(() => { | |
let ticking = false; | |
if (!ticking) { | |
window.requestAnimationFrame(() => { | |
setSize(getSize()); | |
ticking = false; | |
}); | |
ticking = true; | |
} | |
}, []); | |
useEffect(() => { | |
window.addEventListener('resize', handleResize); | |
return () => window.removeEventListener('resize', handleResize); | |
}, []); | |
console.log(size); | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment