Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

290
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda