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

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

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 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