Created
September 9, 2021 17:39
-
-
Save alexcloudstar/2d41a077519b353bdc7f4f7ebaca669f 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 } from 'react'; | |
const useWindowSize = () => { | |
const [windowSize, setWindowSize] = useState<{ | |
width: number | undefined; | |
height: number | undefined; | |
}>({ | |
width: undefined, | |
height: undefined | |
}); | |
useEffect(() => { | |
if (typeof window !== 'undefined') { | |
const handleResize = () => { | |
setWindowSize({ | |
width: window.innerWidth, | |
height: window.innerHeight | |
}); | |
}; | |
window.addEventListener('resize', handleResize); | |
handleResize(); | |
return () => window.removeEventListener('resize', handleResize); | |
} | |
}, []); | |
return windowSize; | |
}; | |
export default useWindowSize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment