DEV Community

Cover image for From Props to Global State: Understanding Redux
Hariharan S J
Hariharan S J

Posted on

From Props to Global State: Understanding Redux

1.Introduction

If you’ve worked on a React project before, you’ve probably faced this frustrating situation:

  • A parent component has some data…

  • A child component needs that data…

  • Then another child needs it too…

So what do we do?

We keep passing props from one component to another.

Parent → Child → Grandchild → Great Grandchild
Enter fullscreen mode Exit fullscreen mode

At first, this seems manageable.

But as your application grows, your component structure slowly turns into a nightmare of endless prop passing.

This problem is called:

Prop Drilling
Enter fullscreen mode Exit fullscreen mode

And this is exactly where Redux enters the picture.

Redux is one of the most powerful state management libraries used in React applications. It helps developers manage shared data in a centralized and predictable way without passing props through multiple layers of components.

Instead of scattering state across different parts of your application, Redux gives you a single global store where your application data lives.

This makes your applications:

  • Easier to scale

  • Easier to debug

  • Easier to maintain

  • More predictable

From authentication systems and shopping carts to large enterprise dashboards, Redux is widely used in modern React applications where managing complex state becomes difficult.

In this blog, we’ll break down Redux in the simplest way possible, understand how it works internally, and explore concepts like stores, actions, reducers, dispatching, and global state with practical examples.

2.What is Redux in React?

Redux is a popular state management library used in React applications to manage and share data across multiple components in a predictable way.

In simple terms, Redux acts like a central storage system for your application’s data.

Instead of passing data manually from one component to another using props, Redux allows all components to access shared data directly from a single global store.

3.Why Do We Need Redux?

In small React applications, managing state using useState and passing props between components works perfectly fine.

But as applications grow larger, problems start appearing:

  • Too many props being passed

  • Complex state management

  • Difficult data sharing

  • Hard-to-maintain component structure

This problem is called:

Prop Drilling
Enter fullscreen mode Exit fullscreen mode

Redux solves this problem by creating one centralized store where all shared data is managed.

4.Understanding Redux with a Simple Analogy

Think of Redux like a bank locker system.

Redux Store = Central Locker
Components = Customers
Actions = Requests
Reducers = Bank Employees
Enter fullscreen mode Exit fullscreen mode

How it works:

  1. A component requests a change

  2. Redux processes the request

  3. Store updates the data

  4. All connected components receive updated data automatically

5.How to Install Redux in React

When building small React applications, Hooks like useState and useContext are often enough for managing state. But as applications grow larger and more complex, managing shared data across multiple components becomes difficult.

This is where Redux comes into the picture.

Redux is a powerful state management library that helps developers manage global application state in a centralized and predictable way.

In modern React applications, developers mainly use:

Redux Toolkit (RTK)
Enter fullscreen mode Exit fullscreen mode

because it simplifies Redux setup and reduces unnecessary boilerplate code.

6.Installing Redux in React

To use Redux in a React application, we mainly need two packages:

  • @reduxjs/toolkit

  • react-redux

Install them using npm:

npm install @reduxjs/toolkit react-redux
Enter fullscreen mode Exit fullscreen mode

7.Understanding the Packages

Package Purpose
@reduxjs/toolkit Simplifies Redux setup
react-redux Connects Redux with React

7.Core Concepts of Redux

Redux mainly works with four important concepts:

Concept Purpose
Store Central place that stores application state
Action Object that describes what happened
Reducer Function that updates state
Dispatch Sends actions to reducers

1. Store

The store is the central place where all application data is stored.

Example:

const store = createStore(reducer);
Enter fullscreen mode Exit fullscreen mode

Think of it like the brain of Redux.

All shared state lives inside the store.

2. Actions

Actions describe what should happen.

Example:

{
  type: "INCREMENT"
}
Enter fullscreen mode Exit fullscreen mode

An action is simply a JavaScript object.

It tells Redux:

“Hey, something happened. Update the state.”

3. Reducers

Reducers decide how the state should change.

Example

function counterReducer(state = 0, action) {
  switch(action.type) {
    case "INCREMENT":
      return state + 1;

    default:
      return state;
  }
}
Enter fullscreen mode Exit fullscreen mode

Reducer:

  • Receives current state

  • Receives action

  • Returns updated state

4. Dispatch

Dispatch sends actions to Redux.

Example:

dispatch({ type: "INCREMENT" });
Enter fullscreen mode Exit fullscreen mode

This tells Redux to update the store.

8.How Redux Flow Works

Redux follows a one-way data flow.

Component
   ↓
Dispatch Action
   ↓
Reducer
   ↓
Store Updates
   ↓
UI Re-renders
Enter fullscreen mode Exit fullscreen mode

This predictable flow makes debugging easier.

Example: Counter App Using Redux

const initialState = {
  count: 0
};

function reducer(state = initialState, action) {
  switch(action.type) {
    case "INCREMENT":
      return {
        ...state,
        count: state.count + 1
      };

    default:
      return state;
  }
}
Enter fullscreen mode Exit fullscreen mode

Dispatching action:

dispatch({ type: "INCREMENT" });
Enter fullscreen mode Exit fullscreen mode

Redux updates the store automatically.

9.What is Global State?

Global state means data that multiple components need access to.

Examples:

  • Authentication

  • Shopping cart

  • Theme mode

  • User profile

  • Notifications

Redux helps manage this shared data efficiently.

10.Redux vs useState

useState Redux
Local component state Global application state
Best for small apps Best for large apps
Simple state handling Complex state management
Data shared using props Data shared through store

11.Real-World Use Cases of Redux

  • E-commerce shopping carts

  • Authentication systems

  • Dashboard applications

  • Social media apps

  • Real-time notifications

  • Theme management

  • Large-scale enterprise applications

12.Final Takeaway

Redux is a powerful state management library that helps React applications manage shared data in a centralized and predictable way.

While React Hooks like useState are perfect for local component state, Redux becomes extremely useful when applications grow larger and multiple components need access to the same data.

Understanding Redux is an important step toward building scalable React applications because it teaches developers how to manage complex application state efficiently and maintain a clean architecture.

Top comments (0)