RTK là cách duy nhất được khuyến nghị để viết Redux hiện đại — createSlice gộp actions+reducer, Immer cho phép write mutable syntax, RTK Query thay thế thunk cho data fetching.
Redux Toolkit (RTK) là bộ công cụ chính thức và là cách duy nhất được khuyến nghị để viết Redux hiện đại — Redux thuần giờ được coi là legacy.
RTK giải quyết 3 vấn đề lớn của Redux thuần:
- quá nhiều boilerplate (actions, action types, reducers, combineReducers riêng biệt) →
createSlicegộp tất cả vào một chỗ. - Immutable update phức tạp (
{...state, nested: {...state.nested, value: newVal}}) → Immer bên trong cho phép viếtstate.nested.value = newVal. - Async logic verbose →
createAsyncThunkxử lý pending/fulfilled/rejected tự động
Thêm RTK Query cho data fetching và caching, cạnh tranh trực tiếp với React Query.
Ví dụ thực tế: 1 feature Redux thuần cần 4-5 files (types, actions, reducer, selectors, thunks), RTK chỉ cần 1 file slice.
Lưu ý: khi migrate từ Redux thuần sang RTK, đừng rewrite toàn bộ — RTK tương thích ngược, migrate từng slice một.
RTK is the only recommended way to write modern Redux — createSlice combines actions+reducer, Immer enables mutable write syntax, RTK Query replaces thunks for data fetching.
Redux Toolkit (RTK) is the official toolset and the only recommended way to write modern Redux — plain Redux is now considered legacy.
RTK solves three major problems with plain Redux:
- excessive boilerplate (separate actions, action types, reducers, combineReducers) →
createSliceconsolidates everything. - Complex immutable updates (
{...state, nested: {...state.nested, value: newVal}}) → Immer allows writingstate.nested.value = newVal. - Verbose async logic →
createAsyncThunkhandles pending/fulfilled/rejected automatically
RTK Query handles data fetching and caching, directly competing with React Query.
Practical example: one Redux feature requires 4-5 files (types, actions, reducer, selectors, thunks); RTK needs just one slice file.
Pitfall: when migrating from plain Redux to RTK, don't rewrite everything at once — RTK is backward-compatible; migrate one slice at a time.