Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. ...
  2.  
  3. class Clock extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. timerID: null,
  8. timer: 0
  9. };
  10. };
  11.  
  12. componentDidMount() {
  13. console.log("Clock", "componentDidMount");
  14. const timerID = setInterval( this.updateTimer, 1000);
  15. this.setState( { timerID : timerID} );
  16. }
  17.  
  18. componentWillUnmount() {
  19. console.log("Clock", "componentWillUnmount");
  20. clearInterval(this.state.timerID);
  21. }
  22.  
  23. updateTimer = () => {
  24. this.setState( {timer: this.state.timer + 1 } );
  25. }
  26.  
  27.  
  28. render() {
  29. return (
  30. <div>
  31. <h1>Clock!</h1>
  32.  
  33. <h3>{this.state.timer} s</h3>
  34. </div>
  35. );
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement