D
D
Dima Morichev2015-09-09 14:54:17
JavaScript
Dima Morichev, 2015-09-09 14:54:17

Query Execution Sequence in SPA React Redux?

I do SPA.
With some events on the client, for example, an interesting product has been noted, there is a need to send certain data of this product to the server and, depending on this, load content into the right part of the SPA. Some interactive.
On clicking on the "Interesting" button, I call a function in which I make a POST request to the server and immediately make a request in action, there are 3 of them. Wanting to get the content of the corresponding product.
The problem is that not everyone gets the data. Those. when you click on the "Interesting" button, data is loaded that calls one action, then you try the same thing several times and only then the data from the other actions is loaded.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xtalen, 2015-09-17
@xom9lk

Didn't quite understand what the problem is. It would be clearer with the button click handler code and the action code.
I will assume that you call action one after another, wrap each call in SetTimeout(()=>{action()}, 0);
It should be something like (not tied to Redux):

onClick(data) {
    rest.post("url to backend", {data})
    .then((response) = > {
        setTimeout(() = > {
            AppDispatcher.dispatch({
                actionType: AppConstants.INTERESTED_1,
                data: response.data
            });
        }, 0);

        setTimeout(() = > {
            AppDispatcher.dispatch({
                actionType: AppConstants.INTERESTED_2,
                data: response.data
            });
        }, 0);

        setTimeout(() = > {
            AppDispatcher.dispatch({
                actionType: AppConstants.INTERESTED_3,
                data: response.data
            });
        }, 0);
    })
    .catch(() => {
        AppDispatcher.dispatch({
            actionType: AppConstants.SERVER_ERROR
        });
    });
}

M
Max Sysoev, 2015-09-17
@ColCh

Probably, you need to use Relay or its equivalent for such things.
Another way - you can wrap 2 requests in one Action Creator; there already execute a queue of requests and after them send one action.
Also, return a Promise object from the Action Creator.
PS I don't recommend ever wrapping the propagation of an action in a zero timeout. Can lead to unpredictable consequences

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question