You can manage async data in redux via two methods
- Via 3rd party liberary like redux thunk
- Unhealthy but simple way : Kind of a ruse
I am not using the second way because it adds additional compelexiy of the middle ware.
Since state of the application changes everytime the state tree changes. I can dispatch onCompletion
action in the callbackasyncLoading
action which is inside the reducer
.
const reducer = (state = initialState, action) => {
let { users } = state;
if (action.type === DONE) {
console.log('-updated-')
return { ...state, loading: false}
} else if (action.type === LOAD_USER) {
loadUser(...).then((user) => {
users.push(user)
}).then(() => store.dispatch(done()));
return { ...state, loading: true }
}
}
And so everytime I ask for asyncAction
.It can set isLoading field to true
and after a while when the promise fullfils I can again set the loading to false
which will return us the new state of the tree.