I'm new to React/Redux. I use a fetch api middleware in Redux app to process the APIs. It's https://github.com/agraboso/redux-api-middleware. I think it's the good way to process async api actions. But I find some cases which can't be resolve by myself.
As the homepage https://github.com/agraboso/redux-api-middleware#lifecycle say, a fetch API lifecycle begins with dispatching a CALL_API action ends with dispatching a FSA action.
So my first case is showing/hiding a preloader when fetching APIs. The middleware will dispatch a FSA action at the beginning and dispatch a FSA action at the end. Both the actions are received by reducers which should be only doing some normal data processing. No UI operations, no more operations. Maybe I should save the processing status in state then render them when store updating.
But how to do this? A react component flow over the whole page? what happen with store updating from other actions? I mean they are more like events than state!
Even a worse case, what should I do when I have to use the native confirm dialog or alert dialog in redux/react apps? Where should they be put, actions or reducers?
Best wishes! Wish for replying.
You can add change listeners to your stores, using either
connect()
from React Redux or the low-levelstore.subscribe()
method. You should have the loading indicator in your store, which the store change handler can then check and update the component state. The component then renders the preloader if needed, based on the state.alert
andconfirm
shouldn't be a problem. They are blocking and alert doesn't even take any input from the user. Withconfirm
, you can set state based on what the user has clicked if the user choice should affect component rendering. If not, you can store the choice as component member variable for later use.I'd like to add something. The real world example uses a field
isFetching
in the store to represent when a collection of items is being fetched. Any collection is generalized to apagination
reducer that can be connected to your components to track the state and show if a collection is loading.It happened to me that I wanted to fetch details for an specific entity that doesn't fit in the pagination pattern. I wanted to have a state representing if the details are being fetched from the server but also I didn't want to have a reducer just for that.
To solve this I added another generic reducer called
fetching
. It works in a similar fashion to the pagination reducer and it's responsibility is just to watch a set of actions and generate new state with pairs[entity, isFetching]
. That allows toconnect
the reducer to any component and to know if the app is currently fetching data not just for a collection but for an specific entity.Am I the only one thinking that loading indicators don't belong in a Redux store? I mean, I don't think it's part of an application's state per se..
Now, I work with Angular2, and what I do is that I have a "Loading" service which exposes different loading indicators via RxJS BehaviourSubjects.. I guess the mechanism is the same, I just don't store the information in Redux.
Users of the LoadingService just subscribe to those events they want to listen to..
My Redux action creators call the LoadingService whenever things need to change. UX components subscribe to the exposed observables...
I'm saving the urls such as::
And then I have a memorised selector (via reselect).
To make the url unique in case of POST, I pass some variable as query.
And where I want to show an indicator, I simply use the getFetchCount variable
I would not say so. I think loading indicators are a great case of UI that is easily described as a function of state: in this case, of a boolean variable. While this answer is correct, I would like to provide some code to go along with it.
In the
async
example in Redux repo, reducer updates a field calledisFetching
:The component uses
connect()
from React Redux to subscribe to the store’s state and returnsisFetching
as part of themapStateToProps()
return value so it is available in the connected component’s props:Finally, the component uses
isFetching
prop in therender()
function to render a “Loading...” label (which could conceivably be a spinner instead):Any side effects (and showing a dialog is most certainly a side effect) do not belong in reducers. Think of reducers as passive “builders of state”. They don’t really “do” things.
If you wish to show an alert, either do this from a component before dispatching an action, or do this from an action creator. By the time an action is dispatched, it is too late to perform side effects in response to it.
For every rule, there is an exception. Sometimes your side effect logic is so complicated you actually want to couple them either to specific action types or to specific reducers. In this case check out advanced projects like Redux Saga and Redux Loop. Only do this when you are comfortable with vanilla Redux and have a real problem of scattered side effects you’d like to make more manageable.
I didn't happen upon this question until now, but since no answer is accepted I'll throw in my hat. I wrote a tool for this very job: react-loader-factory. It's got slightly more going on than Abramov's solution, but is more modular and convenient, since I didn't want to have to think after I wrote it.
There are four big pieces:
const loaderWrapper = loaderFactory(actionsList, monitoredStates);
connect()
returns in Redux), so that you can just bolt it onto your existing stuff.const LoadingChild = loaderWrapper(ChildComponent);
ACTION_SUCCESS
andACTION_REQUEST
, for example). (You could dispatch actions elsewhere and just monitor from the wrapper if you wanted, of course.)The module itself is independent of redux-api-middleware, but that's what I use it with, so here's some sample code from the README:
A component with a Loader wrapping it:
A reducer for the Loader to monitor (although you can wire it differently if you want):
I expect in the near future I'll add things like timeout and error to the module, but the pattern's not going to be very different.
The short answer to your question is: