Sometimes, we want to pass event with parameter with React onClick.
In this article, we’ll look at how to pass event with parameter with React onClick.
How to pass event with parameter with React onClick?
To pass event with parameter with React onClick, we set the onClick
prop to a function that returns another function.
For instance, we write
const clickMe = (parameter) => (event) => {
// ...
};
//...
<button onClick={clickMe(someParameter)} />;
to create the clickMe
function that takes the parameter
parameter and returns a function that takes the event
parameter.
Then we set the onClick
prop of the button to the function returned by clickMe
called with someParameter
, which is the function that has event
as the parameter.
Conclusion
To pass event with parameter with React onClick, we set the onClick
prop to a function that returns another function.