Redux

On this page 7

Redux is an open-source JavaScript library for predictable state management that keeps the entire shared state of an application in a single store and changes it only by dispatching actions that are processed by pure reducer functions. In web applications, and especially in single-page applications built with React, Redux gives large teams one explicit, traceable path for every state change.

Dan Abramov and Andrew Clark released Redux in 2015, drawing on the Flux architecture and on ideas from the Elm language. Today the maintainers recommend writing Redux logic with Redux Toolkit, the official package that removes most of the boilerplate for which early Redux was criticized.

How Redux manages application state

Redux works as a unidirectional data flow: the user interface dispatches an action, the store passes the current state and the action to the root reducer, the reducer returns a new state object, and every subscribed component reads the parts it needs. Nothing else is allowed to modify the state, so any change can be traced back to a specific action.

The design rests on three core principles. There is a single source of truth, meaning one state tree per application. The state is read-only, so the only way to change it is to describe the change with an action. Changes are made with pure functions, which means a reducer must compute its result only from its arguments, without side effects, network calls or random values.

The following table lists the building blocks that every Redux application uses and the role each one plays.

Building blockWhat it isResponsibility
StoreObject created once per applicationHolds state, exposes dispatch, getState and subscribe
ActionPlain object with a type fieldDescribes what happened, for example todos/added
ReducerPure function of state and actionReturns the next state without mutating the old one
DispatchStore methodSends an action into the store
SelectorFunction that reads stateExtracts or derives the data a component needs
MiddlewareWrapper around dispatchHandles side effects, logging and async logic
Core building blocks of Redux

Because reducers never mutate data, Redux can detect changes with a cheap reference equality check. A component re-renders only when the selected value is a different object, which is why immutable updates are not a style preference but a functional requirement.

Redux Toolkit and the modern API

Redux Toolkit, often shortened to RTK, is the officially recommended way to write Redux code. It packages the store setup, reducer creation and asynchronous patterns that developers previously assembled by hand, and it enables useful development checks by default.

The createSlice function generates action creators and a reducer from one definition. Inside it, developers may write code that looks like mutation, such as state.items.push(item), because the Immer library records those changes on a draft and produces a correct immutable copy. The table below compares the classic and the modern approach.

TaskClassic ReduxRedux Toolkit
Store setupcreateStore with manual middlewareconfigureStore with sensible defaults
Reducers and actionsSeparate constants, creators and switch statementscreateSlice generates all three
Immutable updatesManual spread operatorsImmer drafts with mutating syntax
Async requestsHand-written thunkscreateAsyncThunk with pending, fulfilled and rejected
Server data cachingCustom reducers per endpointRTK Query with automatic caching
Development checksOptional add-onsMutation and serializability checks enabled
Classic Redux compared with Redux Toolkit

RTK Query deserves separate attention. It treats data fetched from an API as server cache rather than application state, handles loading flags, deduplicates requests and invalidates cached results through tags, which removes a large share of the hand-written reducers in typical projects.

Connecting Redux to user interface frameworks

Redux itself is framework-agnostic, but it is used mostly with React through the official React Redux binding. The Provider component makes the store available to the component tree, useSelector subscribes a component to a slice of state, and useDispatch returns the dispatch function.

Selectors are where performance is won or lost. A selector that builds a new array on every call forces a re-render after every action, so derived data should be computed with memoized selectors created by createSelector from the Reselect library, which Redux Toolkit re-exports.

Redux also fits server-side rendering. A framework such as Next.js can create a fresh store per request, fill it on the server and serialize the state into the page, after which hydration continues on the client with the same data.

How Redux compares with other state approaches

Redux is one option among several, and choosing it should depend on the kind of state an application holds. The comparison below covers the alternatives that front-end teams most often weigh against it.

ApproachState modelBest fit
ReduxSingle store, actions and reducersLarge apps with complex shared client state
React Context with hooksValues passed down the treeRarely changing data such as theme or locale
ZustandSmall hook-based storesMedium apps wanting less ceremony
MobXObservable, mutable objectsTeams preferring reactive programming
TanStack QueryServer cache keyed by queriesApps dominated by fetched API data
Redux and common alternatives

A common misconception is that React Context replaces Redux. Context is a transport mechanism, not a state manager: every consumer re-renders when the provided value changes, and it offers no middleware, no action log and no time-travel debugging.

When to use Redux and common mistakes

Redux pays off when many components share state that changes often, when several people work on the same codebase, or when the team needs an audit trail of changes. The Redux DevTools extension records every action, shows the state before and after it, and lets developers replay or skip actions.

It adds cost where state is small or mostly local. The following table pairs frequent mistakes with their consequences and fixes.

MistakeConsequenceRemedy
Mutating state in a reducerComponents do not updateReturn new objects or use createSlice
Putting all state in the storeExcess code and re-rendersKeep form and UI state local
Storing non-serializable valuesBroken DevTools and persistenceStore plain data, keep instances outside
Side effects in reducersUnpredictable, untestable logicMove them to thunks or listeners
Duplicated or nested dataInconsistent copiesNormalize state by entity ID
Frequent Redux mistakes and remedies
  • Keep each slice focused on one domain, such as cart, session or filters.
  • Derive computed values with selectors instead of storing them twice.
  • Use RTK Query or a similar tool for server data before writing custom reducers.

Stripped to the essentials

Imagine a shared whiteboard in an office that only one clerk may write on. Anyone who wants a change hands the clerk a note saying what happened, and the clerk updates the board by fixed rules. Everyone reads from the same board, so nobody works with outdated information.

Redux is that whiteboard and clerk for a web application. The board is the store, the notes are actions, and the rules are reducers. Because every change goes through the same desk, it is easy to see who changed what and when, which makes bugs much easier to find.

Where it is applied

Consider an online store whose React front end shows a product list, a filter panel, a mini cart in the header and a checkout page. All four areas depend on the same cart and filter data, and they are maintained by different developers.

The team creates a store with configureStore and three slices: cart, filters and session. Adding a product dispatches a cart/itemAdded action, and both the header badge and the checkout total update from the same state. Product data comes from RTK Query, which caches each category for 60 seconds and refetches after an order is placed.

When a customer reports a wrong total, a developer reproduces the session with Redux DevTools and sees that a discount action was dispatched twice. The fix takes minutes instead of hours, because every state change is recorded as a readable action.