A Pen by Usman Ahmad on CodePen.
Last active
March 24, 2019 11:02
-
-
Save dextar47/7b966ffee2113da19c6530bf9af301a7 to your computer and use it in GitHub Desktop.
Clock in react
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
<div id="app"></div> |
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
class Clock extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
date: new Date() | |
}; | |
} | |
tick() { | |
this.setState({date: new Date()}); | |
} | |
componentDidMount() { | |
this.timer = setInterval( | |
() => this.tick(), | |
1000 | |
); | |
} | |
componentWillUnmount() { | |
clearInterval(this.timer); | |
} | |
render() { | |
return ( | |
<p>Today is {this.state.date.toLocaleTimeString()}</p> | |
); | |
} | |
} | |
ReactDOM.render(<Clock />, document.querySelector('#app')); |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> |
A Pen by Usman Ahmad on CodePen.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment