Redux — Predictable Global State Management for JS Apps
Redux is the original predictable state container for JavaScript apps. Modern Redux uses Redux Toolkit (RTK) which reduces boilerplate 80% and includes RTK Query for server state. Still the standard for large-scale React apps.
What it is
Redux is the original predictable state container for JavaScript applications. It enforces a unidirectional data flow: actions describe what happened, reducers specify how state changes, and the store holds the single source of truth. Modern Redux is written with Redux Toolkit (RTK), which eliminates most of the boilerplate that gave Redux its verbose reputation.
It is designed for large-scale React applications where multiple components need to read and write shared state, and where debugging state transitions via time-travel tooling provides real value.
How it saves time or tokens
RTK's createSlice generates action creators and action types automatically from reducer functions, replacing three separate files with one. configureStore sets up the Redux DevTools extension and default middleware without manual configuration. RTK Query handles data fetching, caching, and cache invalidation, eliminating the need for separate libraries like react-query for server state in Redux-heavy codebases.
How to use
- Install Redux Toolkit and React-Redux.
npm i @reduxjs/toolkit react-redux
- Create a slice with state and reducers.
import { configureStore, createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1; },
decrement: (state) => { state.value -= 1; },
},
});
export const { increment, decrement } = counterSlice.actions;
export const store = configureStore({ reducer: { counter: counterSlice.reducer } });
- Connect the store to your React app.
import { Provider, useSelector, useDispatch } from 'react-redux';
import { store, increment } from './store';
function Counter() {
const count = useSelector((s: any) => s.counter.value);
const dispatch = useDispatch();
return <button onClick={() => dispatch(increment())}>{count}</button>;
}
function App() {
return <Provider store={store}><Counter /></Provider>;
}
Example
Using RTK Query for API data fetching:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemon: builder.query({ query: (name: string) => `pokemon/${name}` }),
}),
});
export const { useGetPokemonQuery } = pokemonApi;
Related on TokRepo
- Coding tools -- Developer libraries and frameworks for frontend and backend.
- Prompt library -- Reusable prompts for generating Redux boilerplate with AI.
Common pitfalls
- Using Redux for local component state that does not need to be shared. Use React's built-in useState for truly local UI state.
- Mutating state directly without Immer. RTK uses Immer under the hood, but only inside createSlice reducers. Direct mutations outside of RTK will break.
- Over-fetching with RTK Query because cache tags are not configured. Define proper tag types and invalidation rules to avoid stale data.
Frequently Asked Questions
For large applications with complex shared state, Redux remains a strong choice because of its predictable data flow, excellent DevTools, and mature ecosystem. For simpler apps, React context, Zustand, or Jotai may be sufficient.
Redux is the core library with store, reducers, and actions. Redux Toolkit (RTK) is the official recommended way to write Redux code. It wraps Redux with utilities like createSlice and configureStore that reduce boilerplate by roughly 80 percent.
Yes. Redux is framework-agnostic. Official bindings exist for React (react-redux), and community bindings exist for Angular, Vue, and Svelte. The core Redux store can be used with any UI layer.
RTK Query is a data fetching and caching tool built into Redux Toolkit. It generates React hooks for API endpoints, handles loading states, caching, cache invalidation, and optimistic updates. It serves a similar role to react-query but integrates directly with the Redux store.
Install the Redux DevTools browser extension. It shows every dispatched action, the resulting state diff, and supports time-travel debugging where you can step backward and forward through state history.
Citations (3)
- Redux Toolkit Documentation— Redux Toolkit reduces Redux boilerplate by roughly 80 percent
- Redux GitHub— Redux is the predictable state container for JavaScript apps
- RTK Query Overview— RTK Query handles data fetching, caching, and invalidation
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.