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

165
Views
How to pass a callback without invoking ist?

I want to pass a callback function. At moment it gets called before it gets passed. What is wrong?

<MyComponent callback={()=> console.log(1)} isDirty ={true} setCallback={()=>{}} />

Declaration

export function MyComponent({ ...props} ) {
  const { setCallback, callback, isDirty } = props
  const cancelCallback = isDirty ? () => setCallback(callback) : callback

  return (
    <>

        <Button onClick={cancelCallback}>Cancel</Button>

    </>
  )
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I replicated it on my system and it doesn't get called. One common mistake is to pass a callback function like this:

callback={console.log(1)}

instead like this:

callback={() => console.log(1)}

With this mistake the callback function gets called immediately.

about 4 years ago · Santiago Trujillo Report

0

you need to update your click handler function.

const cancelCallback = () => {
  isDirty ? setCallback(callback) : callback();
};
about 4 years ago · Santiago Trujillo 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!