Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 41 ยท Getting Started

Your First Redux App

Build two small Redux apps step by step: a counter and a todo list. See state, actions, reducers, selectors and dispatch work together in live React examples.

Time to see Redux move. You will build a counter and then a todo list, following the same five steps each time: define the state, write the reducer, create the store, read state with a selector, and dispatch actions from event handlers. The live examples use React plus a small stand-in for createStore, Provider, useSelector and useDispatch. The component code is exactly what you would write with the real packages, apart from the import lines.

The stand-in block at the top of each example (marked STAND-IN) replaces import { createStore } from 'redux' and import { Provider, useSelector, useDispatch } from 'react-redux'. The real packages cannot be loaded in our editor. Everything below the marker is ordinary application code.

The five steps

flowchart TD
    A["1. Describe the state"] --> B["2. Write the reducer"]
    B --> C["3. Create the store"]
    C --> D["4. Read with useSelector"]
    D --> E["5. Dispatch from handlers"]
    E -->|"state changes, UI updates"| D

Steps 1 to 3 happen once. Steps 4 and 5 repeat for every component.

A counter

State is { value: 0, step: 1 }. Two actions change the value; a third changes the step. The reducer is a switch on action.type, and each case returns a new object instead of changing the old one.

Counter with a Redux store

Click the buttons and watch the count. Notice what the component does not do: it never stores the count itself, never calls setState, and never changes state. It reads, and it describes events. Notice also that useSelector is called twice, once per value. A component re-renders only when a value it selected changes.

What happens on a click

  1. The click handler calls dispatch({ type: 'counter/incremented' }).
  2. The store calls counterReducer(currentState, action).
  3. The reducer returns a new state object; the store saves it.
  4. The store tells every subscriber; each useSelector re-runs its selector.
  5. If a selected value changed, that component renders again.

A todo list

The counter's state was one object with two numbers. Real apps hold collections. The same rules apply, but updating nested data without mutation needs map, filter and spread. (Redux Toolkit makes this much easier; see Immer in Reducers.)

Todo list with add, toggle and remove

Three components, one store. AddTodo keeps the text being typed in local state (nobody else needs it) and only dispatches when the form is submitted. That is a good rule of thumb: keep it local until another component needs it. TodoList and Remaining read the same list but calculate different things; Remaining selects a number, so it re-renders only when the count changes.

The same app with Redux Toolkit

With the real Toolkit, steps 2 and 3 shrink. This is read-only because it needs the package:

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

const todosSlice = createSlice({
  name: 'todos',
  initialState: { nextId: 3, items: [] },
  reducers: {
    added(state, action) {
      state.items.push({ id: state.nextId++, text: action.payload, done: false });
    },
    toggled(state, action) {
      const todo = state.items.find((t) => t.id === action.payload);
      if (todo) todo.done = !todo.done;
    },
    removed(state, action) {
      state.items = state.items.filter((t) => t.id !== action.payload);
    },
  },
});

export const { added, toggled, removed } = todosSlice.actions;
export const store = configureStore({ reducer: todosSlice.reducer });

Compare the action creators: dispatch(added('Buy milk')) replaces the hand-written { type: 'todos/added', payload: 'Buy milk' }, and the switch and the spread copies disappear. You will build up to this in Redux Toolkit.

Common beginner mistakes

  • Mutating state in the reducer (state.items.push(...) in a hand-written reducer). The store sees the same object and subscribers may not update. Return a new object.
  • Forgetting default: return state. Without it, every unrelated action sets the state to undefined.
  • Storing derived data. The remaining count is calculated from items; there is no left field in the state.
  • Putting everything in Redux. The input's text is local state.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a component do to change the Redux state?

    Components describe what happened by dispatching actions. The store runs the reducer and saves the new state.
  2. 2. Why does the reducer need a default case that returns state?

    Every dispatched action reaches the reducer, including the internal init action. Unknown actions must return the current state unchanged.
  3. 3. Why is the 'items left' number not stored in the state?

    Derived data should be computed in a selector. Storing it too means two values that must always agree.
  4. 4. Which text in AddTodo is deliberately kept in local state?

    Only AddTodo needs the half-typed text, so it stays in useState and the store receives just the finished todo.

Summary

  • Build a Redux feature in five steps: state, reducer, store, selector, dispatch.
  • Reducers return new objects, handle unknown actions with default: return state, and never mutate.
  • Components read with useSelector and write with useDispatch; they do not store the shared data themselves.
  • Derive values (counts, filtered lists) instead of storing them.
  • Keep purely local state, such as a half-typed input, in useState.

Next: Redux DevTools and Time Travel