in Education by
How to pass a parameter to an event handler or callback in REACT?

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 arrow function

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

Related questions

0 votes
    What is the difference between HTML and React event handling?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the purpose of callback function as an argument of setState()?...
asked Jun 2, 2021 in General by JackTerrance
0 votes
    How to bind methods or event handlers in JSX callbacks?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    Why should we not update the state directly in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the difference between state and props in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What are props in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is state in React JS?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What are Pure Components in React js1?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    When to use a Class Component over a Function Component?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the difference between Element and Component?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is JSX?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    how to pass callback function in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I use some UserControls which get created and destroyed within my application during runtime (by creating and ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I use some UserControls which get created and destroyed within my application during runtime (by creating and ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
...