Redux Toolkit fundamentals
Redux Toolkit is the recommended modern way to write Redux applications.
The current stable package line for this handbook is Redux Toolkit 2.12.0.
Do not start new code by hand-writing legacy createStore, string action constants, and switch-heavy boilerplate unless you are maintaining old Redux code.
What problem Redux solvesโ
Redux gives a shared external store a predictable event-driven update model.
Redux is useful when state coordination, traceability, tooling, middleware, and cross-feature ownership matter.
Installโ
npm install @reduxjs/toolkit react-redux
Redux Toolkit contains the modern Redux utilities. React-Redux connects the store to React.
Create the storeโ
// app/store.ts
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from '../features/counter/counterSlice'
export const store = configureStore({
reducer: {
counter: counterReducer,
},
})
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
configureStore sets up a Redux store with useful defaults, including development checks and Redux DevTools integration.
Create a sliceโ
// features/counter/counterSlice.ts
import { createSlice, type PayloadAction } from '@reduxjs/toolkit'
type CounterState = {
value: number
}
const initialState: CounterState = {
value: 0,
}
const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment(state) {
state.value += 1
},
decrement(state) {
state.value -= 1
},
incrementBy(state, action: PayloadAction<number>) {
state.value += action.payload
},
},
})
export const { increment, decrement, incrementBy } = counterSlice.actions
export default counterSlice.reducer
Redux Toolkit uses Immer internally, so reducer code may look mutative while still producing immutable Redux state updates.
Slice mental modelโ
increment() creates an action object.
The slice reducer understands that action and calculates the next state.
Provide the store to Reactโ
import { Provider } from 'react-redux'
import { store } from './app/store'
root.render(
<Provider store={store}>
<App />
</Provider>,
)
React-Redux manages the subscription between React components and the external Redux store.
Typed hooksโ
Create application-specific hooks once:
// app/hooks.ts
import { useDispatch, useSelector } from 'react-redux'
import type { AppDispatch, RootState } from './store'
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
export const useAppSelector = useSelector.withTypes<RootState>()
Then components do not repeat store types.
Read state with selectorsโ
function CounterValue() {
const count = useAppSelector((state) => state.counter.value)
return <output>{count}</output>
}
useSelector subscribes to the store and evaluates the selector after dispatches.
By default, React-Redux compares the previous selector result with the next result using strict reference equality.
Dispatch actionsโ
function CounterControls() {
const dispatch = useAppDispatch()
return (
<div>
<button onClick={() => dispatch(decrement())}>-</button>
<button onClick={() => dispatch(increment())}>+</button>
</div>
)
}
The component does not directly mutate store state.
Payload actionsโ
<button onClick={() => dispatch(incrementBy(5))}>
Add 5
</button>
Generated action conceptually resembles:
{
type: 'counter/incrementBy',
payload: 5,
}
Selectors as the read APIโ
Prefer named selectors for important domain reads.
export const selectCounterValue = (state: RootState) => state.counter.value
const count = useAppSelector(selectCounterValue)
For feature architecture:
Components should not need to understand every detail of the root state shape.
Derived selectorsโ
Do not store values that can be derived from existing state without a reason.
export const selectCompletedCount = (state: RootState) =>
state.todos.items.filter((todo) => todo.completed).length
If expensive derived calculations need memoization, use selector tooling such as Reselect/createSelector rather than duplicating state.
Multiple slicesโ
export const store = configureStore({
reducer: {
auth: authReducer,
cart: cartReducer,
editor: editorReducer,
notifications: notificationsReducer,
},
})
Architecture:
Slice boundaries should follow domains, not arbitrary file size.
What belongs in Redux?โ
Good candidates can include:
- shared workflow state across distant features;
- complex client-side editor state;
- normalized entities used by many screens;
- cross-feature events and transitions;
- state that benefits from Redux DevTools traceability;
- state coordinated through middleware.
Poor default candidates:
- one component's modal visibility;
- every text input;
- data already owned by a server-state cache;
- URL parameters that should remain shareable in the URL;
- duplicated derived values.
Redux vs Contextโ
Tree-scoped dependency distribution ยท provider boundaries ยท no built-in selector/event/middleware model.
External store ยท action-driven transitions ยท selector subscriptions ยท middleware ยท DevTools event trace.
Neither is automatically better.
Redux vs Zustandโ
Redux Toolkit is more structured and opinionated.
Zustand is lighter and store-centric with less ceremony.
Choose based on architecture requirements, not bundle-size slogans.
Common mistakesโ
Putting every value in Reduxโ
Global reach does not imply global ownership.
Mutating state outside reducersโ
State updates should flow through reducers/actions or supported RTK APIs.
Returning fresh objects from selectors unnecessarilyโ
const data = useAppSelector((state) => ({
user: state.auth.user,
theme: state.settings.theme,
}))
This creates a new object every selector execution and can trigger updates unless equality/memoization is handled deliberately.
Fetching everything with hand-written thunksโ
For server cache lifecycles, consider RTK Query or TanStack Query instead of manually building loading/cache/invalidation logic.
Debuggingโ
Use Redux DevTools to inspect:
If a component does not update, inspect:
- Was the action dispatched?
- Did the correct reducer handle it?
- Did state actually change?
- Does the selector read the right slice?
- Did the selector return the same reference?
- Is the component under the correct Provider?
Exerciseโ
Build a shopping cart slice that supports:
- add item;
- remove item;
- change quantity;
- clear cart;
- select item count;
- select total price.
Keep totals derived rather than stored unless you can justify independent ownership.
Interview questionsโ
Junior: What are store, action, reducer, dispatch, and selector?
Mid-level: What does createSlice generate, and why can reducer code appear to mutate state?
Senior: What should not live in Redux, and how would you decide whether a feature needs Redux instead of Context or Zustand?
Staff: How would you define Redux domain boundaries across multiple teams without turning the store into a shared dumping ground?