π§© React Redux β Actions, Reducers, and Store (2025 Guide)
π§² Introduction β Why Use Redux in React?
As React applications grow, managing shared and complex state across components becomes challenging. Redux is a predictable state container that helps centralize state, enforce structure, and enable advanced debugging tools like Redux DevTools.
Redux works with actions, reducers, and a central store, making state updates explicit and traceable.
π― In this guide, youβll learn:
- The three core principles: actions, reducers, and the store
- How to integrate Redux with React
- How data flows in a Redux-powered React app
- Real-world patterns and best practices
π§© What Is Redux?
Redux is a unidirectional state management system with three main building blocks:
Concept | Description |
---|---|
Store | Holds the application state |
Actions | Plain JS objects describing what happened |
Reducers | Pure functions that return updated state |
ποΈ 1. Setting Up Redux in React
β Install Dependencies:
npm install redux react-redux
β Optional (for Redux Toolkit):
npm install @reduxjs/toolkit
π§ 2. The Redux Store β Global State Container
β store.js
import { createStore } from 'redux';
import rootReducer from './reducers';
const store = createStore(rootReducer);
export default store;
β Redux Toolkit Alternative:
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
π¦ 3. Actions β Describe What Happened
Actions are plain JS objects with a type
property and optional payload
.
β Example:
export const increment = () => ({ type: 'INCREMENT' });
export const setUser = (user) => ({ type: 'SET_USER', payload: user });
π You dispatch these actions using dispatch(action)
.
π 4. Reducers β Handle State Transitions
Reducers receive the current state and the action, then return a new state (without mutating the original).
β
Example β counterReducer.js
:
const initialState = { count: 0 };
function counterReducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
default:
return state;
}
}
export default counterReducer;
β
Reducers must be pure functions
β
Always return new objects (immutability)
π 5. Connecting Redux to React
β Provide the Store in Your App:
import { Provider } from 'react-redux';
import store from './store';
<Provider store={store}>
<App />
</Provider>
β
Access Redux State with useSelector
:
import { useSelector } from 'react-redux';
const count = useSelector((state) => state.counter.count);
β
Dispatch Actions with useDispatch
:
import { useDispatch } from 'react-redux';
import { increment } from './actions';
const dispatch = useDispatch();
<button onClick={() => dispatch(increment())}>+</button>;
π 6. Redux Toolkit β Simpler Redux Setup
Redux Toolkit (RTK) is the official recommended way to use Redux in 2025.
β Slice Example:
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { count: 0 },
reducers: {
increment(state) {
state.count += 1;
},
},
});
export const { increment } = counterSlice.actions;
export default counterSlice.reducer;
β
Supports mutating syntax with Immer (under the hood)
β
Reduces boilerplate
β
Includes createAsyncThunk
for async actions
π Redux Data Flow Diagram
Component --> dispatch(action)
β
Reducer
β
Store (state)
β
useSelector() β Component
β Data flows one-way through the system
π Best Practices
β
Keep state normalized and flat
β
Use Redux Toolkit to simplify boilerplate
β
Separate actions, reducers, and selectors
β
Avoid putting local component state (e.g., input focus) in Redux
β
Use DevTools to debug and time-travel through actions
π Summary β Recap & Next Steps
Redux gives you a predictable, centralized way to manage application state in React. With clear actions, reducers, and a global store, you gain control, testability, and debuggabilityβespecially in large apps.
π Key Takeaways:
- Redux organizes state with actions, reducers, and a store
- Data flows in one direction, making it predictable
- Use
useSelector
to read state,useDispatch
to update it - Redux Toolkit simplifies setup and improves productivity
- Ideal for large apps with complex, shared state
βοΈ Real-World Relevance:
Used in enterprise-level apps like Jira, Slack, Shopify Admin, and complex dashboards where state predictability and structure are critical.
β FAQ Section
β What is the main difference between Context API and Redux?
β
Context is good for small, simple state sharing. Redux is better for structured, scalable state logic with tooling and middleware.
β Is Redux still relevant in 2025?
β
Yes. Redux is actively maintained and recommended for large-scale apps. With Redux Toolkit, itβs more approachable than ever.
β What triggers a re-render in Redux?
β
When the selected part of the state returned by useSelector
changes.
β Do I need Redux for small apps?
β No. Stick to useState
, useReducer
, or useContext
unless your app has complex shared state.
β Can I use Redux with Hooks only?
β
Yes. Use useSelector
, useDispatch
, and Provider
from react-redux
.
Share Now :