Skip to content

Instantly share code, notes, and snippets.

@roeib
Created May 22, 2019 09:15
Show Gist options
  • Save roeib/7ff034c3b207fd87b64ad139c75fe7fd to your computer and use it in GitHub Desktop.
Save roeib/7ff034c3b207fd87b64ad139c75fe7fd to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
class MyComponent extends UseEffectExample {
constructor(props) {
super(props);
this.state = {
width: window.innerWidth;
};
}
componentDidMount() {
window.addEventListener("resize", this.handleResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.handleResize);
}
handleResize = () =>{
this.setState({ width: window.innerWidth });
}
render() {
return (
<h1>{this.state.width}</h1>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment