Skip to content

Instantly share code, notes, and snippets.

@audiolion
Created August 29, 2018 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audiolion/1eed352ae4152778eb8fc9795e0e9d5f to your computer and use it in GitHub Desktop.
Save audiolion/1eed352ae4152778eb8fc9795e0e9d5f to your computer and use it in GitHub Desktop.
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