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?
Some of the advantages of using Redux are as follows: Redux provides extremely easy state transfer between the components. The states are always predictable in Redux and its maintenance is relatively easy. Debugging and testing code in Redux is simple through logging behaviour and status.
Using Redux for state management implies a trade-off. It introduces complexity, indirection and constraints into your code, which can be frustrating if there is no good reason for doing so. Using Redux also means learning how it works, which again could be a waste of time if you don't need it.
Redux is most useful in cases when:The app state is updated frequently. The logic to update that state may be complex. The app has a medium or large-sized codebase, and might be worked on by many people. You need to see how that state is being updated over time.
Redux provides a solution by ensuring that: Your state is wrapped in a store which handles all updates and notifies all code that subscribes to the store of updates to the state.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With