Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
React curried arrow function in event handlers

How does react know to provide the event as a second argument in the code below?

const clickMe = (parameter) => (event) => {
        event.preventDefault();
        // Do something
}
    
<button onClick={clickMe(someParameter)} />

Does it generate this to:

<button onClick={(event) => clickMe(someParameter)(event)} />

Or how does it work? Thanks.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Maybe this will help explain it a little better.

Closures are functions that carry the information (variables etc.) from their local environment with them when they're returned.

I'll work this example without arrow functions as they can be a little deceiving.

// `multplyBy` accepts a number as its argument
// It returns a new function that "remembers" that number
// when it's returned. But that new function *also*
// accepts a number
function multiplyBy(n) {
  return function(n2) {
    return n2 * n;
  }
}

// So `multiplyBy(5)` returns a new function
// which we assign to the variable `five`
const five = multiplyBy(5);

// And when we call `five` with a number we get
// the result of calling 5 * 10.
console.log(five(10));

If you substitute multiplyBy(n) with clickMe(n) you'll see that you'll get a new function that gets used by the click listener, and the first argument of that function will always be the event.

about 4 years ago · Juan Pablo Isaza Report

0

Clickme variable in your code is a function which has return is function e => {...}. So when you specify like this:

<button onClick={clickMe(someParameter)} />

it is equivalent to

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

which is basic form of a event handler in react

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!