Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React onClick - pass event with parameter

Without Parameter

function clickMe(e){
  //e is the event
}

<button onClick={this.clickMe}></button>

With Parameter

function clickMe(parameter){
  //how to get the "e" ?
}
<button onClick={() => this.clickMe(someparameter)}></button>

I want to get the event. How can I get it?

like image 561
IMOBAMA Avatar asked Mar 04 '17 15:03

IMOBAMA


People also ask

How do you pass parameters to an event handler React?

Passing the event object of react as the second argument. If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

How do you add onClick events in React JS?

Adding Events React events are written in camelCase syntax: onClick instead of onclick . React event handlers are written inside curly braces: onClick={shoot} instead of onClick="shoot()" .


6 Answers

Try this:

<button 
   onClick={(e) => {
      this.clickMe(e, someParameter);
   }}
>
Click Me!
</button>

And in your function:

function clickMe(event, someParameter){
     //do with event
}
like image 150
Jyothi Babu Araja Avatar answered Oct 07 '22 04:10

Jyothi Babu Araja


With the ES6, you can do in a shorter way like this:

const clickMe = (parameter) => (event) => {
    // Do something
}

And use it:

<button onClick={clickMe(someParameter)} />
like image 22
Minh Kha Avatar answered Oct 07 '22 04:10

Minh Kha


Solution 1

function clickMe(parameter, event){
}

<button onClick={(event) => {this.clickMe(someparameter, event)}></button>

Solution 2 Using the bind function is considered better, than the arrow function way, in solution 1. Note, that the event parameter should be the last parameter in the handler function

function clickMe(parameter, event){
}

<button onClick={this.clickMe.bind(this, someParameter)}></button>
like image 45
Bence Dergez Avatar answered Oct 07 '22 04:10

Bence Dergez


Currying with ES6 example:

const clickHandler = param => event => {
  console.log(param); // your parameter
  console.log(event.type); // event type, e.g.: click, etc.
};

Our button, that toggles handler:

<button onClick={(e) => clickHandler(1)(e)}>Click me!</button>

If you want to call this function expression without an event object, then you'd call it this way:

clickHandler(1)();

Also, since react uses synthetic events (a wrapper for native events), there's an event pooling thing, which means, if you want to use your event object asynchronously, then you'd have to use event.persist():

const clickHandler = param => event => {
  event.persist();
  console.log(event.target);
  setTimeout(() => console.log(event.target), 1000); // won't be null, otherwise if you haven't used event.persist() it would be null.
};

Here's live example: https://codesandbox.io/s/compassionate-joliot-4eblc?fontsize=14&hidenavigation=1&theme=dark

like image 38
Alexander Kim Avatar answered Oct 07 '22 02:10

Alexander Kim


To solve the creating new callback issue completely, utilize the data-* attributes in HTML5 is the best solution IMO. Since in the end of the day, even if you extract a sub-component to pass the parameters, it still creates new functions.

For example,

const handleBtnClick = e => {
  const { id } = JSON.parse(e.target.dataset.onclickparam);
  // ...
};

<button onClick={handleBtnClick} data-onclickparam={JSON.stringify({ id: 0 })}>

See https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes for using data-* attributes.

like image 45
Harry Chang Avatar answered Oct 07 '22 03:10

Harry Chang


<Button onClick={(e)=>(console.log(e)}>Click Me</Button>
like image 42
Akash Chhetri Avatar answered Oct 07 '22 02:10

Akash Chhetri