Jaseem Abbas Jaseem Abbas -4 years ago 86
Javascript Question

Redux action not getting called the second time

I have two redux actions which call as follows.

export function action1(params) {
//This line is always called.
return (dispatch) => {
//This line is not called the second time.
return MyApi.call1(params)
.then(response => {
// some logic
return dispatch(someFunction1());
})
.catch(error => {
throw(error);
});
};
}

export function action2(params) {
return (dispatch) => {
return MyApi.call2(params)
.then(response => {
// call the first API again
action1();
return dispatch(someFunction2());
})
.catch(error => {
throw(error);
});
};
}


When the view is first loaded,
action1
is called within the constructor of the view. Upon performing an action and triggering
action2
in the same view,
action1
needs to be called on
action2
's success to get the updated list from the server. Unfortunately, code breaks without any error when
action1
is called the second time.

What am I missing here?

Answer Source

You have not dispatched the action1.

dispatch( action1( params ) )

Invoking action1() without dispatch just returns a function. In order to get dispatch in returned function, you should dispatch that function. Then it will be caught by redux-thunk middleware. The middleware will pass dispatch and invoke function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download