Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux questions about the real-world example

Tags:

reactjs

redux

I have a few questions about redux's real-world example.

  1. 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...

  2. 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?

like image 860
Cheng Avatar asked Oct 16 '15 12:10

Cheng


People also ask

What has been your experience with Redux?

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.

What is one reason you might not want to use Redux in your project?

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.

In which of these cases is Redux the most useful and should be used?

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.

What problem does Redux solve?

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.


1 Answers

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.

like image 136
Dan Abramov Avatar answered Sep 19 '22 09:09

Dan Abramov