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

368
Views
What is correct way to call function inside setState(useState) hooks

I want to update state at real time and call function inside setState but i am getting error.

onClick={() => setDetails(prevState => ({...prevState,id: product.orderId,status: 'processing'}) => updateStatus() )}

enter image description here

Can someone tell me correct syntax and expression

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

0

It seems you are using hooks. You can't call a function in setState like in class components. You need to watch it through useEffect.

React.useEffect(() => {
  updateStatus() 
}, [state.id, state.status])
about 4 years ago · Juan Pablo Isaza Report

0

the function created using useState hook doest have a second argument. it will not support call back function after updating the state. it was only there in the setState function which is used in class components.

You can use useEffect instead

useEffect(()=> {
   updateStatus()
}, [details])

   return (
     ...
    onClick={() => setDetails(prevState => ({...prevState,id: product.orderId,status: 'processing'}))}
     ...
    )

in this way, on every setDetails update it will call useEffect and then updateStatus

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!