Created
September 27, 2019 16:48
-
-
Save yansusanto/1c9998ece3e18315f574a191c74c6add 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, useLayoutEffect } from 'react'; | |
export const useWindowWidth = () => { | |
const [windowWidth, setWindowWidth] = useState(window.innerWidth); | |
const handleWindowResize = () => { | |
setWindowWidth(window.innerWidth); | |
}; | |
useLayoutEffect(() => { | |
window.addEventListener('resize', handleWindowResize); | |
return () => window.removeEventListener('resize', handleWindowResize); | |
}, []); | |
return windowWidth; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment