Skip to content

Instantly share code, notes, and snippets.

@maylisdoucet
Last active May 14, 2018 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maylisdoucet/bff7a793d4e7c18b043ba2a7cce88f0e to your computer and use it in GitHub Desktop.
Save maylisdoucet/bff7a793d4e7c18b043ba2a7cce88f0e to your computer and use it in GitHub Desktop.
Décompte secondes (fil rouge)
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