Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass multiple props in a single event handler in ReactJS?

Tags:

I'm trying to add a onClick listener to a li and the props will be passed to another component that will eventually call a function. My first thought was simply put 2 onClick, but I got a warning for duplicate props.

<li onClick={props.headlinesClicked} onClick={props.getData}>Top Headlines</li>

Then I tried the vahnilla JS way but I assume it's not working because these are expressions rather than actual functions.

 <li onClick={() => {{props.headlinesClicked}; {props.getData}}}>Top Headlines</li>

How can I pass these multiple props in a single onClick?

like image 789
PrettyBiased Avatar asked Mar 12 '18 00:03

PrettyBiased


People also ask

How do you pass multiple props to onClick event React?

You could either do onClick={() => props. headlinesClicked(); props. getData()} , or extract it to a new function/method that does both calls, then invoke it with onClick={myNewMethod} or onClick={() => myNewMethod()} .

Can we send multiple props in React?

If we want to pass/call the multiple props methods in a single event handler in ReactJS then there are two ways to make it work. Method 1: We can make a separate method for the event and call all the props method in that method.

Can we use same event handler on multiple components?

Absolutely yes! You can fire a lightning event and that will be handled in each & every component which has a handler defined in it.

Can we pass multiple props?

Props passing. As the code above demonstrates, you can pass props between components by adding them when the component is being called, just like you pass arguments when calling on a regular JavaScript function .


2 Answers

Your first snippet won't work as you can't bind to a prop multiple times. The second is incorrect firstly because anything inside the brackets is JS, so the additional brackets inside are unnecessary and you're also not invoking any of the functions.

Think of it this way, onClick={props.headlinesClicked} is kind of like a shorthand for onClick={() => props.headlinesClicked()} (not exactly as onClick also passes an event as the first parameter, but hopefully you get the gist of it). So, when you do onClick={() => props.headlinesClicked} you're not actually calling the function, just returning a reference to it which then doesn't actually do anything.

You could either do onClick={() => props.headlinesClicked(); props.getData()}, or extract it to a new function/method that does both calls, then invoke it with onClick={myNewMethod} or onClick={() => myNewMethod()}.

like image 168
Dom Avatar answered Oct 11 '22 23:10

Dom


Have the passed function call the other two functions normally:

<li onClick={() => { props.headlinesClicked(); props.getData() }}>Top Headlines</li>

Alternatively (recommended for readability), make a new function which calls those two functions. Consider making the function a class method if it's a class component.

const handleListItemClick = () => {
  props.headlinesClicked()
  props.getData()
}

return <li onClick={handleListItemClick}>Top Headlines</li>
like image 21
kingdaro Avatar answered Oct 11 '22 22:10

kingdaro