Redux Introduction
Redux Introduction covers single store principles, actions, pure reducers, dispatch, selectors, and when Redux beats Context alone.
Course: React — State Management Lesson: 05
Learning objectives
Understand the Redux state model, actions, reducers, store, dispatch, and selectors, and identify the kinds of applications that benefit from centralized predictable state transitions.
What is Redux?
Redux is a predictable state container commonly used for shared client-side application state. It centralizes state in a store and updates that state through dispatched actions processed by reducers.
Core concepts:
- Store: holds the current application state.
- Action: describes an event or requested state change.
- Reducer: calculates the next state from the current state and action.
- Dispatch: sends an action to the store.
- Selector: reads a value from the store.
The unidirectional data flow
User event|vdispatch(action)|vReducer(currentState, action)|vNext state|vSubscribed UI selects data|vReact renders updated view
This flow makes state transitions explicit and easier to inspect.
A conceptual reducer
const initialState = { count: 0 };function counterReducer(state = initialState, action) {switch (action.type) {case "counter/increment":return { ...state, count: state.count + 1 };case "counter/decrement":return { ...state, count: state.count - 1 };default:return state;}}
A reducer should be pure: for the same state and action, it returns the same next state and does not perform network requests, timers, or unrelated side effects.
Store interaction
In traditional Redux, the store is created with a reducer, components dispatch actions, and selectors read state. Modern Redux applications should generally use Redux Toolkit, which reduces boilerplate and provides recommended defaults.
When Redux is useful
Redux can be useful when many features share client-owned state, transitions are complex, predictable event history is valuable, or the team benefits from explicit centralized state contracts. It is not automatically necessary for a small app or for remote data that is better managed by a server-state library.
Redux vs component state
Component state remains ideal for isolated UI concerns. Redux is for shared client state whose ownership and transitions benefit from centralization. Avoid copying every input field or transient UI detail into Redux without a requirement.
Common mistakes
- Mutating state in traditional Redux reducers.
- Dispatching vague actions such as
setEverything. - Putting API response caching into Redux without a clear reason.
- Treating Redux as mandatory for every React project.
- Creating a global store for values used by one component.
Best practices
Use Redux Toolkit for modern Redux, keep reducers pure, name actions around domain events, read state through selectors, and separate client-state decisions from server-state concerns.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1What are the main Redux principles?
BeginnerModel answer
Single store as source of truth, state updated by dispatching actions, and pure reducers computing the next state.
2What is the difference between dispatch and selector?
BeginnerModel answer
dispatchsends an action to update state. A selector reads derived or sliced state for components.
Intermediate
3What is the role of a reducer?
IntermediateModel answer
A reducer receives current state and an action and returns the next state without side effects.
4When would you choose Redux over Context?
IntermediateModel answer
When shared client-state complexity, middleware, tooling, or selective subscriptions justify a formal store architecture.
Advanced
5Why must reducers be pure?
AdvancedModel answer
Purity keeps transitions predictable, testable, and compatible with time-travel debugging and middleware expectations.
Practice
Model a shopping cart with actions to add an item, remove an item, and clear the cart. Identify which UI state should remain local.