Open xxRockOnxx opened 8 years ago
I'm experiencing the same issue. What is the proper way to dispatch during an asynchronous request?
getUsersList() {
return () => {
axios.get("api/users")
.then((users) => {
this.successfullyRetreivedUsers(users.data);
})
.catch((err) => {
this.errorRetreivingUsers(err);
});
};
}
successfullyRetreivedUsers(users){
return users;
}
errorRetreivingUsers(err){
return err;
}
Weirdly, the documentation is a bit misleading.
Instead of having dispatch()
at the beginning of the function as shown here
return (dispatch) => {
// we dispatch an event here so we can have "loading" state.
dispatch();
We should be using dispatch()
after the Promise has resolved.
So I used this:
fetchUsers() {
return dispatch => {
axios.get('/path/to/resource').then((response) => {
dispatch(response.data);
}).catch((errors) => {/* handle errors */});
}
}
the dispatch()
comes with a payload parameter.
I, then, listened to the fetchUsers directly at my store
handleFetchUsers: UserActions.FETCH_USERS
then
handleFetchUsers(users) {
this.users= users;
}
Thank you for the response. Due to the inconsistent documentation and non-pluggable nature of Alt I ended up moving to Redux.
I'm following the guide from here: http://alt.js.org/guide/async/
This part:
The dispatch() above LocationSource.fetch() returns the error.
Here's how I do my promise:
Also in here:
what i the dispatch doing on the setTImeOut? Isn't that will tell that the async request is complete? Where's the setting/dispatching of "loading" or "will fetch" state here?