Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a onClick to work in a row - reactjs

Tags:

reactjs

I am trying to get a click even to work with a table in reactjs. My first attempt was to make the whole row clickable. Here is my code:

var UserList = React.createClass({
  getInitialState: function() {
    return getUsers();
  },
  handleClick: function(e) {
    console.log("clicked");
  },
  render: function() {
    var users = this.state.users.map(function(user) {
      return (
        <tr onClick={this.handleClick}>
          <td>{user.name}</td>
          <td>{user.age}</td>
          <td></td>
        </tr>
      );
    });
    return(
      <div className="container">
        <table className="table table-striped">
          <thead>
            <tr>
              <th>Name</th>
              <th>Age</th>
              <th>Full Detail</th>
            </tr>
          </thead>
            <tbody>
              {users}
            </tbody>
        </table>
      </div>
    );
  }
});

This did not work. I then tried to add a button in the table:

<button className="btn" onClick={this.handleClick}>Full Detail</button>

That also did not work. I have other onClick's working throughout my app, but how do I make this work with a table?

like image 842
jhamm Avatar asked Aug 12 '14 14:08

jhamm


People also ask

How do you get multiple onClick events in react?

The first solution to perform multiple onClick events in React is to include all of your actions inside of a function and then call that single function from the onClick event handler. Let's explore how to do that in a React Component: import React from 'react'; function App() { function greeting() { console.

How do you call onClick in ReactJS?

The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.

How do I send 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 you trigger two function call onClick in react?

What would be the equivalent for making two function calls onClick in ReactJS? Very simple: pass a function that calls the two functions, just like you would to with ele. onclick = ... or addEventListener .


2 Answers

Your problem is the function of user that creates the table row is not bound to your react component. The value of this will not be your react component and handleClick will not exist as a property of this.

Try

var users = this.state.users.map(function(user) {
  return (
    <tr onClick={this.handleClick}>
      <td>{user.name}</td>
      <td>{user.age}</td>
      <td></td>
    </tr>
  );}.bind(this);
});

Or use Underscore's bind if you want it to work on all browsers.

like image 114
Mark Bolusmjak Avatar answered Nov 07 '22 07:11

Mark Bolusmjak


I'm new to react. How about this? You just wrap it in another function, then that function holds the closure scope and it calls it correctly.

No idea if this is bad practice or the performance difference, but it seems to work...

var users = this.state.users.map(function(user) {
  return (
    <tr onClick={()=>this.handleClick(user)}>
      <td>{user.name}</td>
      <td>{user.age}</td>
      <td></td>
    </tr>
  );}.bind(this);
});
like image 37
Matt Broekhuis Avatar answered Nov 07 '22 07:11

Matt Broekhuis