Redux Toolkit

Last updated: 2026-11-02

Redux Toolkit Redux Toolkit

Redux Toolkit (RTK) is the officially recommended way to write Redux: createSlice generates a reducer and its actions in one step, configureStore wires up the devtools and the middleware for you, and the bundled Immer lets you update state by writing it as if it were mutable. The result is far less boilerplate than the classic hand-written setup, while the single source of truth stays exactly the same.

GroupRouting & State Mgmt
KindLibrary
SinceRedux Toolkit 2.x

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { configureStore, createSlice } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 },
  reducers: {                       // Immer: you can "mutate" directly
    inc: state => { state.value += 1 },
    addAmount: (state, action) => { state.value += action.payload }
  }
})
export const { inc, addAmount } = counterSlice.actions
export const store = configureStore({ reducer: counterSlice.reducer })

⚙️ Parameters

action.payloadThe data carried by a dispatch: in dispatch(addAmount(5)) the payload is 5, and it is declared by the reducer signature of createSlice.

Returns:createSlice returns { reducer, actions }; configureStore returns the store; inside a component useDispatch gives the dispatch function and useSelector selects the state.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

The three buttons dispatch inc, addAmount(10) and reset and the number follows - the Redux mental model of one source of truth plus pure dispatch updates (simulated with useReducer).

💡 Key Points

💡 Tip The reducer of createSlice can "mutate" the state because Immer is built in, yet the result is still an immutable update; never touch the state outside the reducer.
ℹ️ Info Async data belongs to createAsyncThunk or RTK Query; the latter also manages the request cache and can replace useQuery entirely.
⚠️ Warning Putting local UI state (a draft in an input, a modal open flag) into Redux is an anti-pattern: only shared business state belongs in the store, local state stays with useState.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between RTK and classic Redux?
A No hand-written action constants, Immer and thunks are built in and the devtools are configured for you; more than half of the boilerplate disappears and it is the only officially recommended way to write Redux.
Q Does state.value += 1 break immutability?
A Only on the surface: Immer intercepts the assignment and produces an immutable copy, which is equivalent to { ...state, value: state.value + 1 } but reads more naturally.
Q When do I pick RTK over Zustand?
A Choose RTK when the team already knows the Redux ecosystem, needs devtools time travel, wants one middleware convention or works in a large group; choose Zustand when you want the least code and the smallest footprint.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏