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

202
Views
arrow function in jsx
const MyComp = () => <button onClick={() => nagivate('/')}>home</button>

I have code above where I put arrow function inside my onClick handler. My colleague asked me to do this instead:

const MyComp = () => {
   const handleClick = () => nagivate('/')
   return <button onClick={handleClick}>home</button>
}

What is the point of it? he argue we can have performance issue. I'm confused.

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

0

Both versions are equivalent. The handleClick function will we redefined on every render. To improve performance use useCallback(() => nagivate('/'), [nagivate]). Then the function will only change if the navigate function changes.

about 4 years ago · Juan Pablo Isaza Report

0

If you pass anonymous function as event handler it will cause function recomputation(recreation) on each rerender, thus forcing element which exposed you event to also rerender since it "thinks" that new handler is passed because of new function reference of your anonymous event handler. If function recomputation affects your UI performance I suggest you to wrap handler in useCallback and like that you will have stable reference of you handler. At the end I disagree with you colleague that this(one simple button) will have impact on you performance, if it does it means that your upper levels are the ones that needs optimizations. It is cleaner but in terms of performance, in this specific case, almost no difference.

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!