🧠 React State Management
Estimated reading: 4 minutes 37 views

🧩 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:

ConceptDescription
StoreHolds the application state
ActionsPlain JS objects describing what happened
ReducersPure 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧩 React Redux – Actions, Reducers, Store

Or Copy Link

CONTENTS
Scroll to Top