Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leslie-alldridge/6556e83799ae92adc14d9f12c346a552 to your computer and use it in GitHub Desktop.
Save leslie-alldridge/6556e83799ae92adc14d9f12c346a552 to your computer and use it in GitHub Desktop.
window size in react
constructor(props) {
super(props);
this.state = { width: 0, height: 0 };
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
}
componentDidMount() {
this.updateWindowDimensions();
window.addEventListener('resize', this.updateWindowDimensions);
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateWindowDimensions);
}
updateWindowDimensions() {
this.setState({ width: window.innerWidth, height: window.innerHeight });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment