React onClick event React onClick event reactjs reactjs

React onClick event


I think you're going about this wrong, because ReactJS is just JavaScript I don't think your way of firing this event will work. Your onClick should fire a function attached to your React element like so.

var Hello = React.createClass({    myClick: function () {        alert("Hello World!");    },    render: function() {        return <button onClick={this.myClick}>                   Click Me                </button>;    }});React.render(<Hello />, document.getElementById('container'));


Note: this is another way to do it if you want something quick/inline:

<button onClick={()=>{ alert('alert'); }}>alert</button>


If the function to be run has parameters, is has to be bound to the function as follows:

var Hello = React.createClass({  handleClick: function (text) {      alert(text)  },   render: function () {      return <button onClick = {          this.handleClick.bind(null, "Hello World")      } > Click Me < /button>;  }});React.render(<Hello / > , document.getElementById('container'));

This makes sense now. Thanks again @Chris-Hawkes for pointing me in the right direction.