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

140
Views
Need to understand inline function call with react FC

Im quite new to React and have created a new view at my company. I use inline functions because many guides do the same. Everything is written as function components.

I write onClick like this:

onClick: () => onBackClick()

My co-workes says this is a usless arrow function.

Will try to give you the whole picture. Parent component has the onBackClick function declared

const Parent = () => {
     const onBackClick = () => {backOff}
    
          <Child {...{onBackClick}}/>

}

const Child = ({onBackClick}) => {
 return (
   <MyButton {...{onClick: () => onBackClick() }}/>
)

}

My co-workers want me to write it like this:

onClick: onBackClick

I dont get a good explanation on why and really would like to learn. Can someone be kind to explain why this is a better way to write it.

Regards.

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

0

Compare:

const onBackClick = () => { console.log("This does something useful"); };

const onClick = onBackClick;

onClick();

const onBackClick = () => { console.log("This does something useful"); };

const onClick = () => onBackClick();

onClick();

In the first example (your co-worker's way) there is one function and it is passed around.

In the second example (your way) you create a whole new function that does nothing except call the first function.


Aside from being redundant, this creates an extra inefficiency in React.

When the component is re-rendered, the output is checked for changes.

onBackClick might not have changed, but (when using your way) onClick has, so React will delete the old event handler from the DOM and assign a new one with the new (but identical) onClick function you created.

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!