Yes, this is actually a Redux flaw, and not only that - if you use MobX, you will have the same problem. So, the answer is to generalize it somehow - write factory reducers that will take constants as arguments and combine them only once and reuse them for all asynchronous actions.
I created the library just for this problem, so feel free to look at it. Also, just keep in mind that raw Redux is pretty verbose anyway, and try to better understand your domain model. Your example would look like this:
import { createTile } from 'redux-tiles'; const someTile = createTile({ type: ['some', 'example'], fn: ({ api, params }) => api.get('/some/request', params), }); dispatch(actions.some.example(params)); selectors.some.example(state)); // { isPending: true, error: null, data: null }
The data of the last selector will be updated automatically after receiving a response - so you do not need to write such materials manually.
Another problem is nested updates, and you will have to do the same, but with a lot of verbosity that I have always tried to solve. But, of course, this is a fairly simple library that tries to cover simple use - for complex ones, I definitely recommend that you try something individual.
source share