I am quite new to React and Redux. I want to chain multiple API calls using redux-promise-middleware and implemented my actions as follows:
locationActions.js:
import { visitLocation } from '../services/actionService';
export const VISIT_LOCATION = 'VISIT_LOCATION';
export const VISIT_LOCATION_PENDING = 'VISIT_LOCATION_PENDING';
export const VISIT_LOCATION_FULFILLED = 'VISIT_LOCATION_FULFILLED';
export const VISIT_LOCATION_REJECTED = 'VISIT_LOCATION_REJECTED';
const visitLocationAction = (characterId, location) => ({
type: VISIT_LOCATION,
// visitLocation returns a new promise
payload: visitLocation(characterId, location)
});
export { visitLocationAction as visitLocation };
I dispatch this action from my React component using mapDispatchToProps
:
const mapDispatchToProps = dispatch => (
bindActionCreators({ visitLocation }, dispatch)
);
This works! However, I want to dispatch another action after visitLocation
is settled and fulfilled. I can not call Promise.then()
because it doesn't provide the dispatch
method, therefore not "binding" my action to the reducer.
Tutorials mention I should call dispatch(secondAction())
but I do not have dispatch available in my action creators.
Can someone point out to me what am I missing?
Edit:
As suggested in the first answer, I tried the following approach:
import { visitLocation } from '../services/locationService';
import { fetchSomething } from '../services/otherService';
const visitLocationAction = (characterId, location) => {
return (dispatch) => {
return dispatch(visitLocation(characterId, location))
.then(() => dispatch(fetchSomething()));
};
};
export { visitLocationAction as visitLocation };
But I got action:undefined and the following error:
Error: Actions must be plain objects. Use custom middleware for async actions.
at dispatch (redux.js:200)
at redux-logger.js:1