I'm using the Reactable library to display data tables in React and I want to set/change the state of my parent component UsersTable
, when I click on a row in the table, to remember the id of the clicked record for further actions.
Unfortunately I cant' get it done. I try to pass the handleClick
function as a prop but I always get an Uncaught TypeError:
and handleClick
is undefined.
var Table = Reactable.Table;
var Tr = Reactable.Tr;
var Td = Reactable.Td;
var UsersTable = React.createClass({
propTypes: {
users: React.PropTypes.array.isRequired,
},
handleClick: function(row) {
this.setState({userId: row.key})
},
getInitialState: function() {
return {
userId: null
};
},
render: function() {
return (
<div className="usersTable">
<div className="globalUserSearch">
<div className="globalUserSearchLabel">Selection:</div>
<div><input type="text" className="userSearchInput" placeholder="Global Search"></input></div>
</div>
<Table className="table" itemsPerPage={5} sortable={true} handleClick={this.handleClick}>
{this.props.users.map(function(row) {
return (
<Tr key={row.key} onClick={this.props.handleClick.bind(this, row)}>
<Td column="username">{row.personalInformation.username}</Td>
<Td column="firstname">{row.personalInformation.firstname}</Td>
<Td column="lastname">{row.personalInformation.lastname}</Td>
</Tr>
)
})}
</Table>
</div>
)},
});
If I define a function directly at the <Tr>
element as it's described here
and log the row.key
to the console, everything is fine. But as I said, I need to update the state of my parent component.
Any suggestions would be great.
Thx in advance.