Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. // fonction padding : ajoute 0 si < 10
  4. function pad(s) { return (s < 10) ? '0' + s : s; }
  5.  
  6. // Search component created as a class
  7. export default class Horloge extends React.Component {
  8.  
  9. constructor(props) {
  10. super(props);
  11. this.state = {time:this.updatedTime()};
  12.  
  13. // pour avoir accèss a this.setstate dans la fonction
  14. this.tick = this.tick.bind(this);
  15. }
  16.  
  17. componentDidMount() {
  18. this.timer = setInterval(this.tick, 1000);
  19. }
  20. componentWillUnmount() {
  21. clearInterval(this.timer);
  22. }
  23.  
  24. // methode permettant de recuperer la date et l'heure courante parsées
  25. updatedTime(){
  26. var now = new Date();
  27. return { jour : [pad(now.getDate()), pad(now.getMonth()+1), now.getFullYear()].join('/') ,
  28. heures : pad(now.getHours()),
  29. minutes : pad(now.getMinutes()),
  30. secondes : pad(now.getSeconds())
  31. };
  32. }
  33.  
  34. //gestion de l'event du timer
  35. tick(){
  36. var now = new Date();
  37. var newState = {time:this.updatedTime()};
  38. //mise a jour du state
  39. this.setState(newState);
  40. }
  41.  
  42. render() {
  43. return (
  44. <div>
  45. <h3>{this.state.time.jour}</h3>
  46. <h4>{this.state.time.heures}:{this.state.time.minutes}</h4>
  47. </div >
  48. );
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement