Skip to content

Latest commit

 

History

History
43 lines (22 loc) · 2.84 KB

File metadata and controls

43 lines (22 loc) · 2.84 KB

Redux - Combined Reducers

The most common state shape for a Redux app is a plain Javascript object containing "slices" of domain-specific data at each top-level key. Similarly, the most common approach to writing reducer logic for that state shape is to have "slice reducer" functions, each with the same (state, action) signature, and each responsible for managing all updates to that specific slice of state. Multiple slice reducers can respond to the same action, independently update their own slice as needed, and the updated slices are combined into the new state object.

Why choose Redux instead of the Context API for global state?

If you are using Redux only to avoid passing props down to deeply nested components, then you could replace Redux with the Context API. It is exactly intended for this use case. On the other hand, if you are using Redux for everything else (having a predictable state container, handling your application’s logic outside of your components, centralizing your application’s state, using Redux DevTools to track when, where, why, and how your application’s state changed, or using plugins such as Redux Form, Redux Saga, Redux Undo, Redux Persist, Redux Logger, etc…), then there is absolutely no reason for you to abandon Redux. The Context API doesn’t provide any of this.

What is the purpose of a reducer?

In Redux, a reducer is a pure function that takes an action and the previous state of the application and returns the new state. The action describes what happened and it is the reducer’s job to return the new state based on that action

What does an action contain?

an object that have type and paylod

Why do we need to copy the state in a reducer?

redux only requires our reducers to stay pure. If the new state is different, the reducer must create new object, and making a copy is a way to describe the unchanged part.

Document the following Vocabulary Terms

immutable state:

If an object is immutable, any changes that need to be made to it within a function must be made to a copy of the object.

time travel in redux:

is the ability to move back and forth among the previous states of an application and view the results in real time.

action creator:

is merely a function that returns an action object. Redux includes a utility function called bindActionCreators for binding one or more action creators to the store’s dispatch() function.

reducer:

In Redux, a reducer is a pure function that takes an action and the previous state of the application and returns the new state.

dispatch:

dispatch is a function of the Redux store. You call store. dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you.