Skip to main content
InterviewHubPRO
Section Overview
Q112

Question 112 of 407

Part 5: React Redux State Management (101 - 134)

28%
React Core & Hooks (407 Qs)•Part 5: React Redux State Management (101 - 134)

Why are Redux state functions called reducers?

mid LevelMost AskedCode ExampleState Management

Reducers always return the accumulation of the state (based on all previous and current actions). Therefore, they act as a reducer of state. Each time a Redux reducer is called, the state and action are passed as parameters. This state is then reduced (or accumulated) based on the action, and then the next state is returned. You could reduce a collection of actions and an initial state (of the store) on which to perform these actions to get the resulting final state.

Practical Code Example

javascript
// Reducers are named after Array.prototype.reduce:
// Both take (accumulator, currentValue) => newAccumulator:
const actions = [{ type: 'ADD', val: 5 }, { type: 'ADD', val: 10 }];
const finalState = actions.reduce((state, action) => state + action.val, 0);
console.log(finalState); // 15