Skip to content

Instantly share code, notes, and snippets.

@ShizukuIchi
Last active February 6, 2019 01:33
Show Gist options
  • Save ShizukuIchi/c59c83d65e379000126375fd05bb7263 to your computer and use it in GitHub Desktop.
Save ShizukuIchi/c59c83d65e379000126375fd05bb7263 to your computer and use it in GitHub Desktop.
class ExampleWithEffects extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.timer = null;
}
minus = () => {
this.setState(prevState => ({
count: prevState.count - 1
}));
};
componentDidMount() {
this.timer = setInterval(this.minus, 150);
document.title = `You clicked ${this.state.count} times`;
}
componentDidUpdate() {
document.title = `You clicked ${this.state.count} times`;
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
const { count } = this.state;
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => this.setState({ count: count + 1 })}>
Click me
</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment