reading-notes


Project maintained by mr-atta Hosted on GitHub Pages — Theme by mattgraham

Redux - Combined Reducers



Why choose Redux instead of the Context API for global state❓ 📁

Context API is easy to is use as it has a short learning curve. It requires less code, and because there’s no need of extra libraries, bundle sizes are reduced.
Redux on the other hand requires adding more libraries to the application bundle. The syntax is complex and extensive creating unnecessary work and complexity.

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. It may seem simple, but it does have to be a pure function with no side effects.

What does an action contain❓ 📁

Actions are the only source of information for the store as per Redux official documentation.

Why do we need to copy the state in a reducer❓ 📁

If the new state is different, the reducer must create new object, and making a copy is a way to describe the unchanged part.



Redux

Redux


Combined Reducers

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.

As our application grows and becomes more complex, we can have multiple reducers each managing independent parts of the state. The combineReducers() helper function turns an object whose values are different reducing functions into a single reducing function that we can pass to createStore().

🎥 Multiple Reducers Example

📌 Redux Docs: Using Combined Reducers

📌 Redux Docs: Combined Reducer Syntax