Redux actions / reductions or direct stateing

I am new to Redux. I had problems understanding the value of actions and reducers against components that directly modify the repository.

In Redux, React components do not change storage directly. Instead, they send an action - like posting a message. Then the reducer processes the action - sort of like a message subscriber - and changes the state (or rather, creates a new state) in response.

It seems to me that the pub / sub-like interaction adds layers of indirection that make it difficult to understand what the component actually does - why not just let the components directly pass the new state to the Redux store? Would it be bad if you inject something like this.props.setReduxState into the React component?

I am beginning to understand the significance of why the state itself should be unchanged (a related question - Is Redux just an illustrious global state? ) Related to checking for updates to see which props need to be updated in response to state changes. My question is additional action / gear levels or direct store management.

+1
source share
1 answer

During the development process, you often need to know who and how changed the state. A mutating state through the issuance of actions allows you to answer these questions.

Actions are a payload of information that tells the store how to modify it. This information is presented in the form of simple javascript objects that allow you to register, organize and store this information. Since the whole story is "remembered", you can later reproduce the entire chain of actions for debugging or testing. Together with a tool like Redux DevTools , it makes the development process very simple and amazing. Since all changes to the repository are logged on the monitor, you can see how and when the state was changed at each step. Moreover, you can return or go through the chain of actions.

Another advantage that all mutations are centralized in one place is the fact that it is easier to control the condition. This ensures that all mutations occur one after another in a strict order, and no callbacks can make the application behavior unstable. It also allows you to save in one place functionality that is common to some actions, or, in other words, to use middlewares.

+1
source

Source: https://habr.com/ru/post/1267199/


All Articles