import React from 'react' class Clock extends React.Component { constructor(props) { super(props) this.state = { time: new Date() } } componentDidMount() { console.log('mount') this.intervalId = setInterval(this.tick.bind(this), 1000); } componentWillUnmount() { console.log('unmount') clearInterval(this.intervalId); } render() { return (
Time: { this.state.time.toString() }
) } tick() { this.setState({ time: new Date() }); } } export default Clock