Go Back

React: How To Pass An Argument To An Event Handler Function

Posted on 2018-11-05

Sometimes you want to pass an event handler function like this:

<button onClick={doThis}>...</button>

Now you want to give the doThis an argument:

<button onClick={doThis(argument)}>...</button>

This won't run as expected, because the braces will run the function immediately.

Solution

Instead, you can use an arrow function:

<button onClick={() => doThis(argument)}>...</button>