Having a look at the eslint rule, No .bind() or Arrow Functions in JSX Props.
It says not to use, arrow functions or bind:
<div onClick={this._handleClick.bind(this)}></div>
<div onClick={() => console.log('Hello!'))}></div>
But rather use:
<div onClick={this._handleClick}></div>
This is all well and good but how do I pass arguments to this function on the click event?
Here is my naive code example:
export class UserList extends Component {
constructor(props) {
super(props);
this.handleToggleActive = this.handleToggleActive.bind(this);
}
handleToggleActive() {
console.log(arguments);
}
render() {
return (
<ul className="user-list">
{this
.props
.users
.map(user => (
<li key={user.id}>
<Link to={`/users/${user.id}`}>{user.name}</Link>
<Button onClick={this.handleToggleActive}>Toggle Active</Button>
</li>
))}
</ul>
);
}
}
How do I get the user.id
to my handleToggleActive
function? when I try something like:
<Button onClick={this.handleToggleActive(user.id)}>Toggle Active</Button>
It is executed when rendering, and the only other way I can see to do it is to use an arrow function?
What is the correct way to do this?