redux

what is Redux ?

  • Profile picture of Mcs
  • by Mcs July 9, 2025

Redux is a state management library used with React (or any JavaScript app) to manage the application's global state in a predictable way.

Why Use Redux?

In React, local state (useState) works fine for small apps. But in larger apps, managing shared state across many components becomes hard.

Redux provides:

  1. A centralized store to hold all state
  2. A consistent way to update state using actions
  3. Predictable state flow (unidirectional)
  4. Easier debugging, testing, and time travel with DevTools

Redux Core Concepts

ConceptDescription
StoreSingle source of truth (holds the entire app state)
ActionAn object that describes what happened ({ type: 'INCREMENT' })
ReducerA pure function that handles actions and returns a new state
DispatchSends an action to the reducer to update the store
SelectorRetrieves specific state from the store

Redux Flow Diagram

UI (React Component)
     dispatch(action)
Action (e.g. { type: "ADD_USER" })
    
Reducer (function that updates state)
    
New State in Store
    
UI Re-renders with updated data

Installing Redux with React

npm install redux react-redux

Or use Redux Toolkit (recommended):

npm install @reduxjs/toolkit react-redux

Example with Redux Toolkit

1. Create a Slice

// features/counter/counterSlice.js
import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
 name: 'counter',
 initialState: { value: 0 },
 reducers: {
   increment: state => { state.value += 1 },
   decrement: state => { state.value -= 1 },
   addBy: (state, action) => { state.value += action.payload }
 }
});

export const { increment, decrement, addBy } = counterSlice.actions;
export default counterSlice.reducer;

2. Configure the Store

// app/store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';

export const store = configureStore({
 reducer: {
   counter: counterReducer
 }
});

3. Provide the Store in index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { store } from './app/store';
import App from './App';

ReactDOM.render(
 <Provider store={store}>
   <App />
 </Provider>,
 document.getElementById('root')
);

4. Use Redux in Component

import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement, addBy } from './features/counter/counterSlice';

const Counter = () => {
 const count = useSelector(state => state.counter.value);
 const dispatch = useDispatch();

 return (
   <div>
     <h2>Count: {count}</h2>
     <button onClick={() => dispatch(increment())}>+</button>
     <button onClick={() => dispatch(decrement())}>-</button>
     <button onClick={() => dispatch(addBy(5))}>+ Add 5</button>
   </div>
 );
};

When to Use Redux?

Use Redux when:

  1. Many components share state
  2. You need global access (auth, theme, user data)
  3. App grows large and state becomes complex

Don’t use Redux for:

  1. Small apps with limited state
  2. Simple local state (prefer useState, useReducer)

Summary

ConceptDescription
  
TermMeaning
ReduxLibrary for managing global state
StoreCentral place for all state
ActionDescribes what happened
ReducerCalculates the new state
DispatchSends the action to the store