Skip to content

Instantly share code, notes, and snippets.

@kayode-adechinan
Last active June 13, 2019 16:04
Show Gist options
  • Save kayode-adechinan/85a1114d8732be0bd2e7e7636d030e5f to your computer and use it in GitHub Desktop.
Save kayode-adechinan/85a1114d8732be0bd2e7e7636d030e5f to your computer and use it in GitHub Desktop.
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment