A
A
Alex Polyakh2017-08-21 10:16:48
JavaScript
Alex Polyakh, 2017-08-21 10:16:48

Universal fetch, a very large number of requests, with different conditions, how to write correctly?

Bundle of Redux/React
As I understand, it is not correct to describe all requests and actions in one actionCreators.
Can you please tell me how to correctly write actionCreator for requests?
01. The first request received data from the server, depending on what received saddled certain actions;
02. Sent a second request, checked what the server returned, performed certain actions
03 and so on.
Can you please tell me how to correctly compose this functionality?
Now I have everything, the logic is in one actionCreators.
An example of what actionCreators looks like now, I pass it through container, I call it through this.props in the component:
export function fetchApp(url, payload) {
return (dispatch) => {
fetchRequest();
const status = function(response) {
if (response.status !== 200) {
return Promise.reject(new Error(response.statusText));
}
return Promise.resolve(response);
};
const json = function(response) {
return response.json();
};
fetch(url, {
method: 'post',
credentials: 'same-origin'
})
.then(status)
.then(json)
.then(function(data) {
let currentComponent = setComponent(data);
dispatch(fetchSuccess(currentComponent));
console.log('data', data);
})

};
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-08-21
@PolyakhAleksandr

Without delving into the code, logic, and so on, just if you take it for granted that you must run one Action creator (or, for convenience, just an "action") after the result of another, then there is only one important rule: the action must be called inside dispatch functions - and everything will be ok. The main thing is that the action "does not fall out" from the flow of actions going through the dispatcher, in which case it will definitely get into the reducers and everything will be as it should.
ps judging by your code you are doing just that. If it's convenient, break the function into smaller repeating pieces, etc. (but I see that you are already doing this too).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question