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

177
Views
Difference between passing event handler prop as arrow function or function reference

In a class Component, I have something like this:

render() {
  const { onClose } = this.props;

  // later, in the return statement, inside a bunch of other nested markup:
  <CloseIcon onClick={() => onClose()} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path1")} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path2")} className="qmCloseButton" />
}

The close handler prop that I pass in is:

const onCloseModal = urlAfterClose => {
  if (urlAfterClose) {
    history.push(urlAfterClose);
  } else {
    setShowSuccessModal(false);
  }
};

Everything works. However, I always like to avoid arrow functions in my markup when I can, so I try this small change:

  <CloseIcon onClick={onClose} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path1")} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path2")} className="qmCloseButton" />

But for the CloseIcon, the onClose method gets called with a class (looks like it might be the component itself?) instead of being called with no argument. It only works as an arrow function.

What am I missing here?

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

0

It's totally different syntaxes

onClick={onClose} =/= () => onClick={()=>onClose()}

onClick={onClose} in JavaScript this will pass all arguments available to onClose function, which second one runs without params

its same as

onClick={onClose} = onClick={(e)=>onClose(e)}
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!