What is Redux?
Redux is a pattern and library for managing and updating application state, using events called 'actions.' It serves as a centralized store for state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion.
The Three Core Principles
- Single Source of Truth: The global state of your application is stored in an object tree within a single store.
- State is Read-Only: The only way to change the state is to emit an action, an object describing what happened.
- Changes are made with Pure Functions: To specify how the state tree is transformed by actions, you write pure reducers.
Redux Data Flow
Redux uses a 'one-way data flow' app structure. An event triggers an Action, which is Dispatched to the Store. The Store runs the Reducer to calculate the new state, and the UI updates to reflect the change.
Redux Toolkit (RTK) – The Modern Way
In 2026, we no longer write 'vanilla' Redux. We use Redux Toolkit (RTK), which simplifies the process by handling the boilerplate and allowing us to write 'mutating' logic that is actually turned into safe, immutable updates using Immer.
Connecting React to Redux
To use Redux in your React components, you use two primary hooks: useSelector to read data from the store, and useDispatch to send actions to the store.
Redux vs. Context API
| Feature | Context API | Redux Toolkit |
|---|---|---|
| Purpose | Dependency Injection | State Management |
| Performance | Re-renders all consumers | Selective re-renders (Selectors) |
| Debugging | Basic (React DevTools) | Powerful (Redux DevTools) |
| Boilerplate | Very Low | Moderate |
| Middlewares | No native support | Excellent (Thunk, Saga) |