Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 41 ยท Getting Started

What Is Redux?

Learn what Redux is, the problem it solves and its three building blocks: store, actions and reducers. Includes live examples and a clear note on what runs in our editor.

Redux is a small library for keeping the state of an application in one predictable place. Instead of scattering data across many components, you keep it in a single object called the store, and you change it only by sending plain messages called actions. A pure function called a reducer decides what the next state looks like. That is the whole idea.

Redux is not tied to React. It works with any UI and with no UI at all. Most people meet it through Redux Toolkit (RTK), the official, batteries-included way to write Redux today, and React-Redux, the package that connects a store to React components. This course starts at the core, so that Toolkit and React-Redux feel like shortcuts instead of magic.

What is real and what is a stand-in. The packages redux, @reduxjs/toolkit and react-redux cannot be loaded in our in-browser editor. Code that imports them is shown read-only. The runnable examples use a short, clearly labelled stand-in (about 20 lines) that behaves like the real createStore, combineReducers and useSelector. The data flow, the rules and the output you see are what the real library produces. To use the real packages, install them in your own project (see Setting Up a Redux Project).

The problem Redux solves

Imagine a shop page. The cart count is shown in the header, the cart list in a side panel, and the total on the checkout button. When someone adds a product, three places must agree. If each place keeps its own copy, they drift apart. If you pass data and callbacks down through many components, the code becomes tangled.

Redux's answer: there is one source of truth. Everything that needs the cart reads it from the store, and everything that changes the cart does so through the same door. When something looks wrong, you know where to look.

Three building blocks

The store

One object All shared state lives here.
Subscribe Anyone can listen for changes.
Dispatch The only way to change it.
1 / 3

Here is how they fit together:

flowchart LR
    UI["UI: user clicks"] -->|"dispatch(action)"| ST["Store"]
    ST -->|"(state, action)"| RD["Reducer"]
    RD -->|"new state"| ST
    ST -->|"notify subscribers"| UI

The one-way data flow of Redux: the UI never edits the state; it dispatches an action and the store runs the reducer.

A store in 20 lines

Run the example below. createStore here is the stand-in. Look at the three calls you will write all the time: dispatch, getState and subscribe.

JS A tiny store and a counter reducer

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the subscriber also runs for the unknown action: the store notifies after every dispatch, even when the state did not change. Selectors (see useSelector) are what stop that from causing needless work in a UI.

State as a history of events

Because every change is an action, the current state is just the result of replaying all actions from the start. That makes Redux apps easy to debug: log the actions, and you can reproduce any bug. You can even rebuild a state from a list.

JS Replaying actions rebuilds the state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second TryIt contains no store at all, which makes the point: a reducer is a plain function you can call anywhere, and Redux is a thin layer that remembers the latest state and notifies listeners.

The real thing, in Redux Toolkit style

For comparison, this is how the counter looks with the real Redux Toolkit. Do not worry about the details yet: Chapter 3 covers them.

JavaScript
// needs @reduxjs/toolkit (read-only here)
import { configureStore, createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 },
  reducers: {
    incremented(state) {
      state.value += 1;
    },
    added(state, action) {
      state.value += action.payload;
    },
  },
});

export const { incremented, added } = counterSlice.actions;

const store = configureStore({ reducer: counterSlice.reducer });
store.dispatch(incremented());
console.log(store.getState()); // { value: 1 }

Redux in the wider picture

Redux gives you: predictable updates, one place to inspect state, time-travel debugging with the Redux DevTools, and a clear spot for logic that must run when things change (middleware, covered later in the course). It costs you some extra code. The next lesson, When to Use Redux, is about deciding whether that trade is worth it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How can the state in a Redux store be changed?

    State is read-only. The only way to change it is to dispatch an action; the reducer then computes the next state.
  2. 2. Which describes a reducer correctly?

    A reducer is a pure function. It receives the current state and an action and returns the next state, without side effects or mutation.
  3. 3. What is a Redux action?

    An action is a plain object such as { type: 'cart/added', payload: ... }. It is a description of an event, not code that does the work.
  4. 4. How do the live examples in this course relate to the real Redux packages?

    Our editor cannot load npm packages, so real Redux code is read-only and the live examples use small hand-written stand-ins.

Summary

  • Redux keeps shared state in one store and changes it only through dispatched actions.
  • Reducers are pure functions (state, action) => newState; they never mutate or cause side effects.
  • A store offers getState, dispatch and subscribe; it notifies subscribers after every dispatch.
  • State equals the reduction of all past actions, which makes debugging and replay possible.
  • In this course the live code uses labelled stand-ins; real redux, @reduxjs/toolkit and react-redux code is read-only.

Next: When to Use Redux