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

161
Visualizações
react dispatch function after useSelector variable available

I want to dispatch a function after useSelector function get called.

This is my code

 const profile = useSelector(
    ({ x}) => x.profile,
  );


  useEffect(() => {
     dispatch(fetchX(y, setXStatus)); // this is getting called before profile is available 
  }, [dispatch,y]);

The issue is when dispatch(fetchX(y, setXStatus)); getting executed profile variable is not properly set.Is there a way to run fetchX after profile is set.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If by "after useSelector function is called" you mean "whenever profile changes," you need to add profile to the dependency array. If profile can be null (or similar) and you need a profile before you can do the dispatch, you'll need a guard.

Something along these lines:

const profile = useSelector(
    ({ x}) => x.profile,
);

useEffect(() => {
    if (profile) { // <−−−−−− Guard
        dispatch(fetchX(y, setXStatus));
    }
}, [dispatch, y, profile]);
//               ^^^^^^^−−−−− Dependency

Note, though, that this effect callback will be called every time dispatch, y, or profile changes. dispatch probably never changes, but I don't know about y or profile.

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