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

283
Views
State awaiting in React - I'm having trouble understanding the error while using 'useCallback()'

I'm struggling to understand what I'm doing here.

const updateStatus = (r, e) => {
    setShowCustomMessage(false);
    setShowConfirmSave(true);
    // axios logic here
};

is being called from:

<select
   id="update_status"
   onChange={e => handleUpdateStatus(r, e)}
   value={r.status_id}
</select>

I need to await until the setShowCustomMessage() has completed before moving on to the setShowConfirmSave() call.

I have tried using a callback:

const handleUpdateStatus = useCallback(
    (r, e) => {
        updateStatus(r, e);
    },
    [updateStatus, showCustomerMessage]
);

But I am having issues understanding what's going on here as I keep getting a range of errors here, no matter how I try to solve them.

React Hook "useCallback" is called conditionally. React Hooks must be called in the exact same order in every component render.

React Hook useCallback has an unnecessary dependency: 'showCustomerMessage'. Either exclude it or remove the dependency array. Outer scope values like 'showCustomerMessage' aren't valid dependencies because mutating them doesn't re-render the component.

I am trying to understand what's going on here, my understanding of the useCallback hook is that I can give it some dependancies, that on those dependancies changing, it should fire that callback, which then it turns fires the "updateStatus()' function from within the useCallback function.

Thank you.

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

0

If you want to wait for the state to change before doing x you need to use useEffect to watch for the change in that particular state, and then do x.

const updateStatus = (r, e) => {
  setShowCustomMessage(false);
};

useEffect(() => {
  setShowConfirmSave(true);
  // axios logic here
}, [showCustomMessage]);
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!