0 votes
in ReactJS by

How to pass a parameter to an event handler or callback?

1 Answer

0 votes
by

You can use an arrow function to wrap around an event handler and pass parameters:

<button onClick={() => this.handleClick(id)} />

This is an equivalent to calling .bind:

<button onClick={this.handleClick.bind(this, id)} />

Apart from these two approaches, you can also pass arguments to a function which is defined as array function

<button onClick={this.handleClick(id)} />
handleClick = (id) => () => {
    console.log("Hello, your ticket number is", id)
};

Related questions

0 votes
asked Jul 2, 2019 in ReactJS by Venkatshastri
0 votes
asked Jul 2, 2019 in ReactJS by Venkatshastri
...