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 component tutorial with click handler – javascript version

on Feb 12, 2016

This tutorial is javascript version is React component tuorial with click handler – jsx version. It is recommended that you visit that tutorial before reading this. Even though JSX is easier to read, it is still a good idea to be familiar with corresponding javascript syntax. It can also help in Javascript debugging in Chrome.

Here is the screenshot of final outcome:
react-component-with-click-handler-example-javascript-version

Basic React component with props and click handler – javascript version

Here is the html and javascript code (withou jsx) used in this tutorial and how it renders:

<script src="https://fb.me/react-0.14.7.js"></script>
<script src="https://fb.me/react-dom-0.14.7.js"></script>
<div id="mylist"></div>

<script type="text/javascript">
var MyList = React.createClass({
  getInitialState: function () {
    return {count: parseInt(this.props.count || "2") };
  },
  handleClick: function (event) {
    this.setState({count: this.state.count + 1});
  },
  render: function () {
    var count = this.state.count;
    return React.DOM.div(
      null,
      "total items: ",
      count,
      React.DOM.ul(
        null,
        Array(count).join().split(",").map((x, i) => React.DOM.li(
          {key: i}, "item ", i
        ))
      ),
      React.DOM.button(
        {onClick: this.handleClick}, "click to add"
      )
    );
  }
});
ReactDOM.render(React.createElement(MyList, {count: "3"}), document.getElementById('mylist'));
</script>
refresh done
try it online

Few points to note

  1. Instead of React.createElement("div",... we are using React factory method React.DOM.div. It helps improve code readability.
  2. 1st argument to React.DOM.div is properties and 2nd and subsequent arguments to React.DOM.div are taken as content of that div element.
  3. We pass count 3 to MyList ({count: "3"}) which can be accessed using this.props.count.
  4. getInitialState is used to get initial state. It looks for props passed to component. If nothing passed, then it uses default values.
  5. render is called initially and whenever state is changed.
  6. We attached a click handler on button “click to add”. The handler changes the state and increments the count.
  7. Since changes are done in virtual dom and then applied in real dom. It help reduce browser reflow and repaint and makes rendering fast.

Suggested posts:

  1. React jsx hello world with offline transformation
  2. React basic clock example using setInterval
  3. Javascript sleep implementation
  4. Python itertools imap examples
  5. Multiple onload handlers using vanilla Javascipt
  6. Ubuntu – check if a service is upstart based
  7. Python xrange examples
  8. Ubuntu – find where will a package be installed from
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