Redux Toolkit — The Official Modern Way to Write Redux Logic
Redux Toolkit is the official, opinionated, batteries-included toolset for efficient Redux development. It eliminates boilerplate with createSlice, handles immutability with Immer, and includes RTK Query for API caching.
What it is
Redux Toolkit (RTK) is the official, opinionated, batteries-included toolset for efficient Redux development. It eliminates the boilerplate that made Redux painful: no more hand-written action creators, action types, or switch-case reducers. createSlice generates all of these from a single configuration object, and Immer handles immutability under the hood.
It targets React developers who use Redux for state management and want to write less code with fewer bugs. It is also suitable for teams migrating from legacy Redux codebases.
How it saves time or tokens
Traditional Redux requires writing action type constants, action creator functions, and reducer switch statements separately. RTK's createSlice reduces this to a single object with a name, initial state, and reducer functions. RTK Query replaces hand-written API fetching logic (thunks, loading states, caching) with a declarative API definition. A typical Redux feature that took 4 files now fits in 1.
How to use
- Install Redux Toolkit:
npm install @reduxjs/toolkit react-redux. - Create a slice with
createSlice()that defines state shape and reducers. - Configure the store with
configureStore()and provide it to your React app.
Example
import { createSlice, configureStore } from '@reduxjs/toolkit'
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1 },
decrement: (state) => { state.value -= 1 },
addAmount: (state, action) => { state.value += action.payload },
},
})
export const { increment, decrement, addAmount } = counterSlice.actions
const store = configureStore({
reducer: { counter: counterSlice.reducer },
})
Related on TokRepo
- Coding tools — Frontend frameworks and development tools
- Featured workflows — Discover popular developer workflows
Common pitfalls
- Immer's 'mutative' syntax in createSlice only works inside reducer functions. Writing
state.value = xoutside a reducer mutates state directly and causes bugs. - RTK Query cache invalidation requires proper tag definitions. Without tags, cached data can become stale without automatic refetching.
- Migrating a large legacy Redux codebase to RTK should be incremental. Convert one slice at a time rather than rewriting everything at once.
Frequently Asked Questions
Yes. The Redux team officially recommends Redux Toolkit for all new Redux code. The legacy createStore API is deprecated. RTK's configureStore and createSlice are the standard approach for Redux development.
RTK Query is a data fetching and caching layer built into Redux Toolkit. You define API endpoints declaratively, and RTK Query handles fetching, caching, refetching, loading/error states, and cache invalidation. It eliminates the need for manual thunks and loading state management.
Yes. RTK has excellent TypeScript support with strong type inference. createSlice automatically infers action types and payload types. configureStore infers the root state type. The Redux team provides comprehensive TypeScript usage guides.
Immer lets you write code that appears to mutate state directly (state.value = 5) but actually produces a new immutable state object. RTK uses Immer inside createSlice reducers, so you get immutability guarantees without manually spreading state objects.
RTK is better for large applications with complex state, normalized data, or teams already using Redux. Zustand is lighter and simpler for small-to-medium apps. RTK Query gives Redux an advantage for applications with significant API interaction and caching needs.
Citations (3)
- Redux Toolkit GitHub Repository— Redux Toolkit is the official recommended approach for Redux
- Redux Toolkit Official Docs— RTK documentation and tutorials
- Redux Style Guide— Redux style guide recommending RTK
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.