Created
October 15, 2019 19:28
-
-
Save jaysoo/12835078676974cf564226614555d993 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'; | |
export function useViewportSize() { | |
const getSize = () => { | |
return { width: window.innerWidth, height: window.innerHeight }; | |
}; | |
const [size, setSize] = useState(getSize); | |
useEffect(() => { | |
const handleResize = () => setSize(getSize()); | |
window.addEventListener('resize', handleResize); | |
return () => window.removeEventListener('resize', handleResize); | |
}, [getSize]); | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment