Created
August 29, 2018 01:51
-
-
Save audiolion/1eed352ae4152778eb8fc9795e0e9d5f 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 React from "react"; | |
let hasWindow = typeof window !== "undefined"; | |
class WindowSize extends React.Component { | |
state = { | |
width: hasWindow && window.innerWidth, | |
height: hasWindow && window.innerHeight, | |
}; | |
resize = () => | |
this.setState({ | |
width: window.innerWidth, | |
height: window.innerHeight | |
}); | |
componentDidMount() { | |
window.addEventListener("resize", this.resize); | |
} | |
componentWillUnmount() { | |
window.removeEventListener("resize", this.resize); | |
} | |
render() { | |
return this.props.children(this.state); | |
} | |
} | |
export default WindowSize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment