Created
August 2, 2022 06:34
-
-
Save isNan909/ec5b5ea291f3866088a03994bcfe0f56 to your computer and use it in GitHub Desktop.
useWindowSize hook
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 [windowSize, setWindowSize] = useState({ | |
| width: undefined, | |
| height: undefined | |
| }); | |
| useEffect(() => { | |
| function handleResize() { | |
| setWindowSize({ | |
| width: window.innerWidth, | |
| height: window.innerHeight | |
| }); | |
| } | |
| window.addEventListener("resize", handleResize); | |
| handleResize(); | |
| return () => window.removeEventListener("resize", handleResize); | |
| }, []); | |
| return windowSize; | |
| }; | |
| //const windowSize = useWindowSize(); use it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment