|
|
|
|
|
by troebr
3722 days ago
|
|
I just wrote our API layer with redux-saga too, I started with thunks but I didn't like dispatching thunks and actions, also it was hard to reason about flow and side effects:
If I update my search form, the results need to be updated, but the action is "update form", not "update form and update search results", the search result update is a side effect more than something that should be actually fired from the update form. Saga is making side-effects (workflows) possible by subscribing to events. So in this case I have a saga waiting for 'SEARCH_FORM_UPDATE', then it waits for 300ms, then starts the ajax request flow: a request start action, then a request complete action (or request error). The code reads almost like synchronous code once you understand yield. It also allows you to listen to actions that you do not control: we wanted to do something whenever a specific redux-router action happened. You can't do this with thunks, we would have needed a middleware to do this. With sagas you can simply hook up a flow to the action. |
|