Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for click events in React Hooks for passing arguments

This always worked great for me in React class components:

let myValue: string = "Hello World";
<Button onClick={this.handleClick.bind(this, myValue)}></Button>

I found this syntax in the React Hooks documentation and I like it, but it does not always pass back a value:

<Button onClick={handleClick} value={myValue}></Button>

This syntax works, but is hard to type and looks messy:

<Button onClick={() => handleClick(myValue)}></Button>

This is yet another way that works with hooks, but seems hacky to me.

  <Button onClick={handleClick.bind(null, myValue)}></Button>

I'm confused by too many choices. Is there not just some best practice way of doing this?

like image 456
Lambert Avatar asked Apr 04 '19 15:04

Lambert


People also ask

How do you pass arguments on onClick in React?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How do you call onClick in React hooks?

From your code, just cut the handleClick method and paste it below the hook. That will make the function run Everytime the button is clicked.

How do you get onClick value in React?

We set the onClick prop on the button element. Every time the button is clicked, the handleClick function is invoked. To get the value of the input field on button click, we simply access the message state variable in our handleClick function.


2 Answers

This is perfectly suitable way to do this:

<Button onClick={() => handleClick(myValue)}></Button>

The only downside is that onClick prop has new value on each render and triggers a re-render of child component - even if it's pure. This may or may not cause performance problems depending on specific component; this will be a problem if there are many Button instances or their re-renders are expensive.

If a value is static, a callback can be defined as constant function outside a component:

// outside function component
const myValue = "Hello World";
const myHandleClick = () => handleClick(myValue);
...
// inside function component
<Button onClick={myHandleClick}></Button>

If a value is dynamic and is available only inside a component, a function can be defined inside a component and memoized with useMemo or useCallback hook (as another answer already mentions):

// inside function component
const myHandleClick = useCallback(() => handleClick(myValue), [myValue]);
...
<Button onClick={myHandleClick}></Button>
like image 75
Estus Flask Avatar answered Oct 22 '22 05:10

Estus Flask


The first and last versions in your question both create a new function, so if you can get away with just giving the function handleClick to a prop, you should do that.

If you need to pass arguments to the function you can still use your first version in function components, but since this is not used, you can just set it to null.

<Button onClick={handleClick.bind(null, myValue)}></Button>
like image 38
Tholle Avatar answered Oct 22 '22 04:10

Tholle