React & Modern Frontend (445 Qs)•Part 10: Modern React 18, React 19 & Next.js Patterns (317 - 353)
Why is Redux Toolkit recommended over Redux?
Redux Toolkit (RTK) is the official, opinionated way to write Redux logic today. It's built on top of plain Redux and re-exports its APIs, but wraps them with utilities that remove most of the boilerplate and footguns of hand-written Redux.
| Problem with plain Redux | How Redux Toolkit fixes it |
|---|---|
Verbose store setup (combineReducers, manual middleware wiring) | configureStore() sets up the store, redux-thunk, and Redux DevTools automatically |
| Hand-written action types/creators and switch-based reducers | createSlice() generates action types, action creators, and a reducer from a single object |
| Reducers must return new state immutably (easy to mutate by mistake) | Uses Immer internally, so you can write "mutating" logic that's safely converted to immutable updates |
| Async logic needs extra middleware setup (thunk/saga boilerplate) | createAsyncThunk() standardizes async action creators with pending/fulfilled/rejected states |
| Repeated normalization code for lists/entities | createEntityAdapter() provides prebuilt reducers/selectors for normalized state |
| No built-in data-fetching/caching layer | RTK Query (built on top of RTK) handles caching, refetching, and loading/error states for API calls |
Example: plain Redux vs. Redux Toolkit
Plain Redux:
javascript
const INCREMENT = "counter/increment";
const increment = () => ({ type: INCREMENT });
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case INCREMENT:
return { ...state, value: state.value + 1 }; // must copy manually
default:
return state;
}
}
Redux Toolkit:
javascript
import { createSlice, configureStore } from "@reduxjs/toolkit";
const counterSlice = createSlice({
name: "counter",
initialState: { value: 0 },
reducers: {
increment: (state) => {
state.value += 1; // "mutation" is safe — Immer produces new state
},
},
});
export const { increment } = counterSlice.actions;
const store = configureStore({ reducer: { counter: counterSlice.reducer } });
In short, Redux Toolkit is recommended because it enforces best practices by default (immutability, DevTools, sensible middleware), drastically cuts boilerplate via createSlice/createAsyncThunk, and is what the official Redux docs now recommend for any new Redux code.