Redux is a predictable state management library for JavaScript apps (commonly used with React). It helps manage application state in a single, centralized store, making state changes predictable and traceable.
⚙️ Redux Architecture
Redux follows a unidirectional data flow:
- Store
- A single JavaScript object that holds the entire application state.
- Example:
{ user: {...}, posts: [...] }
- Actions
- Plain objects describing what happened.
- Must have a
typeproperty, can havepayload. -
Example:
{ type: "ADD_TODO", payload: { id: 1, text: "Learn Redux" } }
- Reducers
- Pure functions that take current state + action and return new state.
-
Example:
function todoReducer(state = [], action) { switch(action.type) { case "ADD_TODO": return [...state, action.payload]; default: return state; } }
- Dispatch
- Method to send actions to the store, triggering the reducer.
-
Example:
store.dispatch({ type: "ADD_TODO", payload: { id: 1, text: "Learn Redux" } });
- Selectors / mapStateToProps
- Used to read specific slices of state from the store.
🔄 Unidirectional Flow
Component --> Dispatch Action --> Reducer --> New State --> Component
- Component triggers an action.
- Action is sent to the store via
dispatch(). - Reducer calculates the new state.
- Store updates, and the component reacts to state changes.
🧩 Key Features of Redux
- ✅ Single source of truth → easier to debug.
- ✅ Predictable state updates → only via actions & reducers.
- ✅ Time-travel debugging → can replay actions.
- ✅ Works with any UI library (React, Angular, Vue, etc.).
In short:
Redux centralizes your app state in a single store, updates it predictably via actions & reducers, and ensures components always have consistent, traceable state.