Created
July 17, 2018 17:07
-
-
Save FerJSsilva/7752487f6939e22d81601bc5307297de to your computer and use it in GitHub Desktop.
A simple React Clock using moment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import moment from 'moment'; | |
class Clock extends Component { | |
state = { | |
dateTime: moment.utc().format('ddd DD MMM HH:mm:ss'), | |
} | |
componentDidMount() { | |
this.timerId = setInterval(this.tick, 1000); | |
} | |
componentWillUnmount() { | |
clearInterval(this.timerID); | |
} | |
tick = () => { | |
this.setState({ | |
dateTime: moment.utc().format('ddd DD MMM HH:mm:ss'), | |
}); | |
} | |
render() { | |
const { dateTime } = this.state; | |
return ( | |
<span>{dateTime}</span> | |
); | |
} | |
} | |
export default Clock; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment