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

115
Visualizações
useEffect not triggering correctly when used with dispatch

Is there any better solution to this when the first useEffect fires, only getAccountDetailsData() will be successful, getErrorLogInfo() will fail because store.AccountDetails.ID is null at that stage. getAccountDetailsData() will get that value.

Now the use effect does not fire again, but waits 60 seconds to fire the 2nd one which fires every minute. and then everything works fine.

The problem is that I have to wait 1 minutes before I get getErrorLogInfo() to work. if I put a dependency list [] in the first useEffect() it will fire in loops. I won't have to wait for 1 minute but it will trigger a 429 error

  useEffect(() => {
      dispatch(getAccountDetailsData(user.EmailAddress)); // we call this to get AccountDetails.ID
      dispatch(getErrorLogInfo(store.AccountDetails.ID)); // will not work missing AccountDetails.ID
  }, []); // I can put dependency list [] but will be endless loop


//now I have to wait 60 seconds to try again

  useEffect(() => {
    
      dispatch(getAccountDetailsData(user.EmailAddress)); works
      dispatch(getErrorLogInfo(store.AccountDetails.ID)); works
    }, 60000);// refresh data after every 60 seconds
    return () => clearInterval(interval);
  }, [store]);
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You'll need to put a guard statement on your useEffect callback to prevent it from attempting to getErrorLogInfo before store.AccountDetails.ID is set.

Also split up to using two useEffect statements since they will have different dependencies:

useEffect(() => {
  if (user?.EmailAddress) // checks if it exists before running the dispatch
    dispatch(getAccountDetailsData(user.EmailAddress));
}, [user?.EmailAddress]) // gets called once upon mount and if `user.EmailAddress` changes

useEffect(() => {
  if (store?.AccountDetails?.ID) // checks if it exists before running the dispatch
    dispatch(getErrorLogInfo(store.AccountDetails.ID))
}, [store?.AccountDetails?.ID]) // gets called once upon mount and if `user.EmailAddress` changes
about 4 years ago · Santiago Trujillo Relatório

0

if you want to fetch data depending on store data u can do it with 2 useEffect like this:

useEffect(() => {
  dispatch(getAccountDetailsData(user.EmailAddress));
}, [])
 
useEffect(()=>{
if(store.AccountDetails){ 
   dispatch(getErrorLogInfo(store.AccountDetails.ID));
  }
},[store.AccountDetails])
about 4 years ago · Santiago Trujillo 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