Created
February 10, 2020 13:23
-
-
Save KitsonBroadhurst/59ca6a68620b1a2b15c8f833dee42ce8 to your computer and use it in GitHub Desktop.
useWindowSize custom hook
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 React, { useState, useLayoutEffect } from 'react' | |
const useWindowSize = () => { | |
const [size, setSize] = useState([0, 0]) | |
useLayoutEffect(() => { | |
const updateSize = () => { | |
setSize([window.innerWidth, window.innerHeight]) | |
} | |
window.addEventListener('resize', updateSize) | |
updateSize() | |
return () => window.removeEventListener('resize', updateSize) | |
}, []) | |
return size | |
} | |
export default useWindowSize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Part of walkthrough: https://medium.com/@kitson.broadhurst/re-render-view-based-on-browser-size-with-a-react-custom-hook-7401c0371b7a