Skip to content

Instantly share code, notes, and snippets.

@nma
Created February 22, 2017 21:31
Show Gist options
  • Save nma/33f8057e4899bdb55440a693a02c431b to your computer and use it in GitHub Desktop.
Save nma/33f8057e4899bdb55440a693a02c431b to your computer and use it in GitHub Desktop.
ReactJS get window sizes
constructor(props) {
super(props);
this.state = { height: 512 };
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
}
componentDidMount() {
this.updateWindowDimensions();
window.addEventListener("resize", this.updateWindowDimensions.bind(this));
}
componentWillUnmount() {
window.removeEventListener("resize", this.updateWindowDimensions.bind(this));
}
updateWindowDimensions() {
this.setState({ width: window.innerWidth, height: window.innerHeight });
}
@jflessau
Copy link

Works like a charm. Thanks!

@Hossik
Copy link

Hossik commented Jul 30, 2020

Thank You

@SherAtrium
Copy link

Thank you) works very well!) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment