Last active
February 6, 2019 01:33
-
-
Save ShizukuIchi/c59c83d65e379000126375fd05bb7263 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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