Created
December 10, 2018 05:22
-
-
Save leslie-alldridge/6556e83799ae92adc14d9f12c346a552 to your computer and use it in GitHub Desktop.
window size in react
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
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