Last active
May 14, 2018 07:30
-
-
Save maylisdoucet/bff7a793d4e7c18b043ba2a7cce88f0e to your computer and use it in GitHub Desktop.
Décompte secondes (fil rouge)
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
import React, {Component} from 'react'; | |
class MyTimer extends Component { | |
constructor(props) { | |
super(props) | |
this.state = {temps: 0} | |
} | |
timer() { | |
this.setState({ | |
temps: this.state.temps + 1 | |
}) | |
if(this.state.temps > 100) { | |
clearInterval(this.interval); | |
} | |
} | |
componentDidMount() { | |
this.interval = setInterval(this.timer.bind(this), 1000); | |
console.log('Le composant a été mis à jour'); | |
} | |
componentWillUnmount(){ | |
clearInterval(this.interval); | |
console.log('Le composant va se mettre à jour'); | |
} | |
render() { | |
return( | |
<div>Vous êtes sur ce site depuis : {this.state.temps} secondes. </div> | |
); | |
} | |
} | |
export default MyTimer; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment