Skip to content

Instantly share code, notes, and snippets.

@Siemko
Created November 7, 2019 15:05
Show Gist options
  • Save Siemko/c20afc77ae2c46ad9ab2c639d088e936 to your computer and use it in GitHub Desktop.
Save Siemko/c20afc77ae2c46ad9ab2c639d088e936 to your computer and use it in GitHub Desktop.
Class component
import React, { Component } from "react";
class WindowSize extends Component {
state = {
width: window.innerWidth,
height: window.innerHeight
};
handleResize = () => this.setState({
width: window.innerWidth,
height: window.innerHeight
});
componentDidMount() {
window.addEventListener("resize", this.handleResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.handleResize);
}
render() {
const { width, height } = this.state;
return (
<>
<h2>Window size: {width} x {height}</h2>
</>
);
}
}
export default WindowSize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment