Created
November 12, 2017 12:24
-
-
Save saurabhpati/cc4a326936096e91f4b9620f306fac04 to your computer and use it in GitHub Desktop.
React state clock implementation
This file contains 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 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