Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda