Skip to content

Instantly share code, notes, and snippets.

@anmolsukki
Created May 4, 2020 00:58
Show Gist options
  • Save anmolsukki/4310e9490645aaef67dece68ae69e916 to your computer and use it in GitHub Desktop.
Save anmolsukki/4310e9490645aaef67dece68ae69e916 to your computer and use it in GitHub Desktop.
[ ReactJs ] Find Screen size (https://codesandbox.io/s/screensizes-8mrig)
import React from "react";
class ScreenSizes extends React.Component {
constructor() {
super()
this.state = {
width: 0,
height: 0
}
}
componentDidMount = () => {
this.updateWindowDimensions();
window.addEventListener('resize', this.updateWindowDimensions);
}
componentWillUnmount = () => {
window.removeEventListener('resize', this.updateWindowDimensions);
}
updateWindowDimensions = () => {
this.setState({ width: window.innerWidth, height: window.innerHeight });
}
render() {
return (
<div>
<p>Screen Width: <b>{this.state.width}</b></p>
<p>Screen Height: <b>{this.state.height}</b></p>
</div>
)
}
}
export default ScreenSizes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment