Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React onClick event on component

I have a React component called <SensorList /> that has many child <SensorItem />s (another React component). I want to be able to declare an onClick event on each <SensorItem /> from within <SensorList />. I have tried doing the following:

sensorSelected: function(sensor) {     console.log('Clicked!'); },  render: function() {     var nodes = this.state.sensors.map(function(sensor) {         return (             <SensorItem onClick={ this.sensorSelected } />         );     }.bind(this));      return (         <div className="sensor-list">             { nodes }         </div>     ); } 

Needless to say, I do not get any "Clicked!" coming up in my console. The React inspector in Chrome indicates that an onClick event is registered, with the above function body as it should be.

I conclude, therefore, that I can't register onClick events on the actual <SensorItem /> tags (I'm not sure why this is, however). How do I go about achieving this otherwise?

like image 543
GTF Avatar asked Feb 01 '15 22:02

GTF


People also ask

How do you add onClick events to React component?

Adding EventsReact events are written in camelCase syntax: onClick instead of onclick . React event handlers are written inside curly braces: onClick={shoot} instead of onClick="shoot()" .

How do you add onClick to a div in React?

To set an onClick listener on a div element in React:Set the onClick prop on the div. The function you pass to the prop will get called every time the div is clicked. You can access the div as event. currentTarget .

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.


2 Answers

This depends on your SensorItem component's definition. Because SensorItem isn't a native DOM element but, like you said, another React component, onClick as defined here is simply a property of that component. What you need to do is, inside of the SensorItem component pass the onClick prop to an DOM component's onClick event:

var SensorItem = React.createClass({   render: function() {     return (       <div className="SensorItem" onClick={this.props.onClick}>        ...       </div>     );   } }); 
like image 150
Bogdan Dumitru Avatar answered Sep 21 '22 17:09

Bogdan Dumitru


Problem

The problem, as being explained in another answer, is that onClick on a <SensorItem> React component (contrary to native DOM element like <div> or <p>) is treated as passing of component property, and not of a DOM event handler. And as most likely your <SensorItem> component doesn't declare onClick property, that property value simply gets lost.

Solution

The most straightforward solution is to add onClick property explicitly on SensorItem component, then pass it to the root DOM element of that component:

function SensorItem({ prop1, prop2, onClick }) {   (...)    return (     <p onClick={onClick}>       (...)     </p>   ); } 

But the solution that usually works best for me is to group all the undefined component's properties using object destructuring notation, then pass them all to the root DOM element within that component. This way you can pass onClick, onHover, className etc. without needing to define separate properties for each one of them:

function SensorItem({ prop1, prop2, ...rootDOMAttributes }) {   (...)    return (     <p {...rootDOMAttributes}>       (...)     </p>   ); } 

No matter which of the two approaches you use, the handler will now work, as it'll now be attached to the root DOM element of SensorItem:

<SensorItem onClick={...} /> 
like image 25
Robert Kusznier Avatar answered Sep 20 '22 17:09

Robert Kusznier