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

207
Vistas
Redux not updating right after dispatch in functional components

Recently I decided to change my code from class to functional components. I'm using redux, axios and axiosMiddleware. Since I started changing to functional components I noticed something strange. See the following example which is a request using a dispatch (I'm using connect and mapDispatchToProps):

const getUserActivity = () => {
 await props
      .get(displayDateString)
      .then((response) => {
        console.log(response); //prints response
        console.log(props.activity); //prints part of the redux that should be updated
      })
      .catch((error) => console.log('error ' + error));
}
const mapDispatchToProps = (dispatch) => ({
  get: (date) => dispatch(ActivityReportActions.get(date))
})

My problem here is, when I get the response after "then" the redux does not get immediately updated, only afterwards. "console.log(props.activity)" prints the previous state, and in some cases I prefer to access the redux right after the response. Does anyone have any idea why this is happening in functional components and not in class components? If yes, is there a solution which does not require using useEffect()?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is impossible to read new values from props immediately after dispatching. React hasn't had a chance to re-render yet.

If you need to read the latest state immediately after a dispatch, you'll have to write the logic as a thunk, which has access to getState:

https://redux.js.org/usage/writing-logic-thunks#accessing-state

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