Created
November 7, 2019 15:06
-
-
Save Siemko/a05d5dd775dd76298eee1a778fb71536 to your computer and use it in GitHub Desktop.
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
import React, { useState, useEffect } from "react"; | |
const WindowSize = () => { | |
const [{width, height}, setSize] = useState({ window.innerWidth, window.innerHeight }); | |
useEffect(() => { | |
const handleResize = () => setSize({ window.innerWidth, window.innerHeight }); | |
window.addEventListener("resize", handleResize); | |
return () => { | |
window.removeEventListener("resize", handleResize); | |
}; | |
}, [setSize]); | |
return ( | |
<> | |
<h2>Window size: {width} x {height}</h2> | |
</> | |
); | |
}; | |
export default WindowSize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment