Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React onClick not firing

The following code doesn't work. The onClick events are never fired but I see no errors in browserify or console.

var React = require('react');

var Button = React.createClass({
  render: function() {
    return (
      <div className="Button">
        <span className="left" onclick={function() {alert("left")}}>Left</span>
        <span className="right" onclick={function() {alert("right")}}>Right</span>
        <span className="middle" onclick={function() {alert("middle")}}>Middle</span>
      </div>
    );
  }
});

module.exports=Button;

I use alert just for testing small CSS. Why isn't onclick firing?

like image 610
Megh Parikh Avatar asked Jun 02 '15 11:06

Megh Parikh


2 Answers

You have a typo in your example. Use 'onClick' instead of 'onclick'.

<span className="left" onClick={function() {alert("left")}}>Left</span>

see jsfiddle for a working example - https://jsfiddle.net/Ltdc8qpr/1/

like image 112
Jerome WAGNER Avatar answered Sep 20 '22 01:09

Jerome WAGNER


Here with arrow function syntax, triggering the event with onClick handler.

<span className="left" onClick={() => {alert("left")}}>Left</span>
like image 27
Anupam Maurya Avatar answered Sep 21 '22 01:09

Anupam Maurya