Skip to content

Instantly share code, notes, and snippets.

@Siemko
Created November 7, 2019 15:05
Embed
What would you like to do?
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