Skills2026年4月14日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install dcff567b-37be-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Redux Toolkit eliminates Redux boilerplate with createSlice, handles immutability via Immer, and includes RTK Query for API caching.
§01

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.

§02

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.

§03

How to use

  1. Install Redux Toolkit: npm install @reduxjs/toolkit react-redux.
  2. Create a slice with createSlice() that defines state shape and reducers.
  3. Configure the store with configureStore() and provide it to your React app.
§04

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 },
})
§05

Related on TokRepo

§06

Common pitfalls

  • Immer's 'mutative' syntax in createSlice only works inside reducer functions. Writing state.value = x outside 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.

常见问题

Is Redux Toolkit the recommended way to use Redux?+

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.

What is RTK Query?+

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.

Can I use Redux Toolkit with TypeScript?+

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.

How does Immer work in Redux Toolkit?+

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.

Should I use Redux Toolkit or Zustand?+

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.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产