I have one reducer for Clients, one other for AppToolbar and some others...
Now lets say that I created a fetch action to delete client, and if it fails I have code in the Clients reducer which should do some stuff, but also I want to display some global error in AppToolbar.
But the Clients and the AppToolbar reducers do not share the same part of the state and I cannot create a new action in the reducer.
So how am I suppose to show global error? Thanks
UPDATE 1:
I forget to mention that I use este devstack
UPDATE 2: I marked Eric's answer as correct, but I have to say that solution which I am using in este is more like combination of Eric and Dan's answer... You just have to find what fits you the best in your code...
Erik’s answer is correct but I would like to add that you don’t have to fire separate actions for adding errors. An alternative approach is to have a reducer that handles any action with an
error
field. This is a matter of personal choice and convention.For example, from Redux
real-world
example that has error handling:write custom Middleware to handle all the api related error. In this case your code will be more cleaner.
The approach I'm currently taking for a few specific errors (user input validation) is to have my sub-reducers throw an exception, catch it in my root reducer, and attach it to the action object. Then I have a redux-saga that inspects action objects for an error and update the state tree with error data in that case.
So:
And then adding the error to the state tree is as Erik describes.
I use it pretty sparingly, but it keeps me from having to duplicate logic which legitimately belongs in the reducer (so it can protect itself from an invalid state).
what I do is I centralize all error handling in the effect on a per effect basis
If you want to have the concept of "global errors", you can create an
errors
reducer, which can listen for addError, removeError, etc... actions. Then, you can hook into your Redux state tree atstate.errors
and display them wherever appropriate.There are a number of ways you could approach this, but the general idea is that global errors/messages would merit their own reducer to live completely separate from
<Clients />
/<AppToolbar />
. Of course if either of these components needs access toerrors
you could passerrors
down to them as a prop wherever needed.Update: Code Example
Here is one example of what it might look like if you were to pass the "global errors"
errors
into your top level<App />
and conditionally render it (if there are errors present). Using react-redux'sconnect
to hook up your<App />
component to some data.And as far as the action creator is concerned, it would dispatch (redux-thunk) success failure according to the response
While your reducer could simply manage an array of errors, adding/removing entries appropriately.
You can use axios HTTP client. It already has implemented Interceptors feature. You can intercept requests or responses before they are handled by then or catch.
https://github.com/mzabriskie/axios#interceptors