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.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install dcff567b-37be-11f1-9bc6-00163e2b0d79 --target codexÀ exécuter après confirmation du plan en dry-run.
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.
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Fil de discussion
Actifs similaires
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.
Immer — Immutable State with Mutable Syntax
Immer lets you create the next immutable state by mutating the current one. Write mutating code as if it were normal JS and Immer produces a new immutable object via structural sharing. Built into Redux Toolkit and Zustand.
Foundry — Blazing Fast Ethereum Development Toolkit in Rust
Foundry is a portable, modular toolkit for Ethereum smart contract development written in Rust. It includes Forge for testing, Cast for chain interaction, Anvil for local node simulation, and Chisel for an interactive Solidity REPL. Foundry compiles and runs tests significantly faster than JavaScript-based alternatives.
Redux-Saga — Side Effect Management for Redux
Redux-Saga is a middleware library for Redux that uses ES6 generators to handle complex asynchronous side effects like data fetching, caching, and race conditions in a testable and declarative way.