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
Chaining redux action creator Promise.then() in react component

I'm planning on using axios for async calls and promises in my redux action creators. Currently there is not yet an API which can be used so I am keeping things locally.

How can I make the following chainable from inside a react component?

Action creators

export function fetchResultsIfNeeded() {
    return (dispatch, getState) => {
        if (true) {
            return dispatch(fetchResults(day, hash));
        }

        else {return Promise.resolve();}
    };
}

export function fetchResults(day, hash) {
    return (dispatch) => {
        // Dispatch something here

        return new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
            // Do some stuff here
            // Dispatch some other stuff here as well
        }); // Want to chain on this promise in react component
    };
}

React component

import { fetchResultsIfNeeded } from 'actions';

// Class based react component
checkResults(periodFrom, periodTo) {
    this.props.fetchResultsIfNeeded([periodFrom, periodTo]).then(() => {/* Do some stuff here */});
}

// Connected via react redux
export default connect(null, { fetchResultsIfNeeded })(component);

Currently receiving the following error, can it be that it is still waiting on react-redux or so?

Uncaught TypeError: Cannot read property 'then' of undefined

Put the action creator and component in a Pen (not sure how to show full code otherwise)

Full component code, Full action creator code

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I assume you are using redux-thunk. You need to return from the resolve function inside your action creator. The correct flow should be:

export function fetchResults(day, hash) {
    return (dispatch) => {
        
        dispatch(...).then(
             ( ) => {
                 // Do some stuff here
                 return dispatch(...)
             }
        )            
    }
}

over 4 years ago · Santiago Trujillo Denunciar

0

There's more than one way to do this, but perhaps the simplest way to do so with your existing code is to use bindActionCreators:

import { fetchResultsIfNeeded } from 'actions';
import { bindActionCreators } from 'redux';

// Class based react component
checkResults(periodFrom, periodTo) {
    this.props.fetchResultsIfNeeded([periodFrom, periodTo]).then(() => {/* Do some stuff here */});
}

// Connected via react redux
export default connect(
  null,
  dispatch => ({
    ...bindActionCreators({ fetchResultsIfNeeded }, dispatch)
  })
)(component);
over 4 years ago · Santiago Trujillo 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