Skip to content

Instantly share code, notes, and snippets.

@saurabhpati
Created November 12, 2017 12:24
Show Gist options
  • Save saurabhpati/cc4a326936096e91f4b9620f306fac04 to your computer and use it in GitHub Desktop.
Save saurabhpati/cc4a326936096e91f4b9620f306fac04 to your computer and use it in GitHub Desktop.
React state clock implementation
import React from 'react'; // npm install react
import ReactDOM from 'react-dom'; // npm install react-dom
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
};
}
render() {
return (
<div>
<h2>Hello World Its {this.state.date.toLocaleString()}</h2>
</div>
);
}
componentDidMount() {
this.timerId = setInterval(() => { this.tick() }, 1000)
}
componentWillUnmount() {
this.clearInterval()
}
}
let tick = () {
this.setState({
date: new Date()
});
}
ReactDOM.render(<Clock/>, document.getElementById('clock'));
export default Clock;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment