Created
April 10, 2019 22:32
-
-
Save antoniocapelo/19cd86cf9677258e54732ca606faa3b9 to your computer and use it in GitHub Desktop.
Custom React Hook for setting a window resize listener and get updated dimensions
This file contains hidden or 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
const useWindowSize = () => { | |
const [dimensions, setDimension] = useState({ | |
width: window.innerWidth, | |
height: window.innerHeight, | |
}); | |
useEffect(() => { | |
const handler = () => { | |
const {innerWidth, innerHeight} = window; | |
setDimension({ width: innerWidth, height: innerHeight}) | |
}; | |
window.addEventListener('resize', handler); | |
return () => window.removeEventListener(handler); | |
}, []); | |
return { | |
width: dimensions.width, | |
height: dimensions.height, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment