Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS onClick event handler

I have

var TestApp = React.createClass({
      getComponent: function(){
          console.log(this.props);
      },
      render: function(){
        return(
             <div>
             <ul>
                <li onClick={this.getComponent}>Component 1</li>
             </ul>
             </div>
        );
      }
});
React.renderComponent(<TestApp />, document.body);

I want to color the background of the clicked list element. How can I do this in React ?

Something like

$('li').on('click', function(){
    $(this).css({'background-color': '#ccc'});
});
like image 845
user544079 Avatar asked Oct 05 '22 17:10

user544079


People also ask

Is onClick an event handler?

In the case of the" onClick" event handler, which executes a piece of JavaScript when an element is clicked on, it can only be added to visible elements on the page such as <a>, form buttons, check boxes, a DIV etc.

How do I add a click event to React component?

To add the click event in React using plain JavaScript, you need to use addEventListener() to assign the click event to an element. Create one <button> element as ref props so that it can be accessed to trigger the click event.

How do I beat onClick as a prop?

When you add an onClick prop to the LinkButton component, it is just a property of an object. By calling props. onClick from inside of that component you are just calling a function that is stored inside of a property, similar to this: let props = { onClick: function () { alert("Executed!"); } }; props.

Can we use onClick on a component?

Event handlers such as onClick can be used in both functional and class components, the former being more straight-forward, while the latter requires the binding of the handlers to this .


1 Answers

Why not:

onItemClick: function (event) {

    event.currentTarget.style.backgroundColor = '#ccc';

},

render: function() {
    return (
        <div>
            <ul>
                <li onClick={this.onItemClick}>Component 1</li>
            </ul>
        </div>
    );
}

And if you want to be more React-ive about it, you might want to set the selected item as state of its containing React component, then reference that state to determine the item's color within render:

onItemClick: function (event) {

    this.setState({ selectedItem: event.currentTarget.dataset.id });
    //where 'id' =  whatever suffix you give the data-* li attribute
},

render: function() {
    return (
        <div>
            <ul>
                <li onClick={this.onItemClick} data-id="1" className={this.state.selectedItem == 1 ? "on" : "off"}>Component 1</li>
                <li onClick={this.onItemClick} data-id="2" className={this.state.selectedItem == 2 ? "on" : "off"}>Component 2</li>
                <li onClick={this.onItemClick} data-id="3" className={this.state.selectedItem == 3 ? "on" : "off"}>Component 3</li>
            </ul>
        </div>
    );
},

You'd want to put those <li>s into a loop, and you need to make the li.on and li.off styles set your background-color.

like image 104
ericsoco Avatar answered Oct 08 '22 22:10

ericsoco