Reputation: 17954
I have a few questions about redux's real-world example.
Unlike the async example where ajax calls are made directly using dispatch, the real-world example uses middleware to deal with this. Which method is recommended when using redux in an react app? and why?
My guess is that middleware is reusable, so if multiple ajax calls need to be made, one general purpose ajax calling middleware is enough as long as different api path are passed in as parameters. But the samething can be said with dispatch...
When do middlewares get executed? By looking at the source code and reading the doc, my understanding is: dispatch an action -> all middlewares get executed , ajax calls can be made here and the returned json data can be put inside the action object and pass it onto the reducers-> reducers get executed
. Am I correct?
Upvotes: 7
Views: 1430
Reputation: 268323
Unlike the async example where ajax calls are made direactly using dispatch, the real-world example uses middleware to deal with this. Which method is recommended when using redux in an react app? and why?
Use what you like. Different people have different preferences. Some want terse code like what middleware provides, others prefer explicitness and sparseness.
When do middlewares get executed? By looking at the source code and reading the doc, my understanding is: dispatch an action -> all middlewares get executed , ajax calls can be made here and the returned json data can be put inside the action object and pass it onto the reducers-> reducers get executed. Am I correct?
This sounds correct. Each middleware can be asynchronous and pass actions to the next middleware. By the time they reach the reducer, they need to be plain objects. Async Flow and Middleware docs mention this.
Upvotes: 8