1class Clock extends React.Component {
2 constructor(props) {
3 super(props);
4 this.state = {date: new Date()};
5 }
6
7 componentDidMount() {
8 this.timerID = setInterval(
9 () => this.tick(),
10 1000
11 );
12 }
13
14 componentWillUnmount() {
15 clearInterval(this.timerID);
16 }
17
18 tick() { this.setState({ date: new Date() }); }
19 render() {
20 return (
21 <div>
22 <h1>Bonjour, monde !</h1>
23 <h2>Il est {this.state.date.toLocaleTimeString()}.</h2>
24 </div>
25 );
26 }
27}
28
29ReactDOM.render(
30 <Clock />,
31 document.getElementById('root')
32);