InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

React Tutorials

  • Overview
  • Hello world (jsx)
  • jsx to javascript (babel cli)
  • jsx offline transformation
  • Hello world (javascript)
  • click hander (jsx)
  • click handler (javascript)
  • clock example
  • ajax
  • React FAQ
 
  • Home
  • > Tutorials
  • > Javascript
  • > React

React basic clock example using setInterval

on Feb 16, 2016

To create a basic clock in React, we can use setInterval in componentDidMount and optionally clear it in componentWillUnmount if relevant. You may also want to read React – Component Specs and Lifecycle. Here is the code snippet:

<script src="https://fb.me/react-0.14.7.js"></script>
<script src="https://fb.me/react-dom-0.14.7.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>

<div id="mycomp"></div>
<script type="text/babel">
var MyComp = React.createClass({
  getInitialState: function(){
    return {date:  (new Date()).toLocaleTimeString()};
  },
  componentDidMount: function() {
    this.timerId = setInterval(this.update_time, 1000);
  },
  update_time: function() {
    this.setState({date: (new Date()).toLocaleTimeString()});
  },
  render: function() {
    return (<div>{this.state.date}</div>);
  },
  componentWillUnmount: function() {
    clearInterval(this.timerId);
  },
});
ReactDOM.render(<MyComp/>, document.getElementById('mycomp') );
</script>
refresh done
try it online

Suggested posts:

  1. Javascript how to stop event propagation
  2. AngularJS simple controller tutorial to increment a value
  3. React component tutorial with click handler – javascript version
  4. React javascript render hello world
  5. CSS transform – skew (skewX and skewY)
  6. React jsx hello world with offline transformation
  7. PHP array map example
  8. React – JSXTransformer vs babel – which one should be used?
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Javascript, React, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress