π οΈ React Redux Toolkit β Simplified Redux for Modern State Management (2025 Guide)
π§² Introduction β Why Redux Toolkit?
Redux is a powerful but often boilerplate-heavy state management library. To simplify setup and improve productivity, the Redux team introduced the Redux Toolkit (RTK)βnow the standard way to write Redux logic in 2025.
Redux Toolkit streamlines the Redux process by:
- π« Eliminating boilerplate code
- β Enabling mutation-style logic with Immer
- π§ Providing built-in support for async actions
- π‘ Encouraging modular, slice-based architecture
π― In this guide, youβll learn:
- How Redux Toolkit simplifies actions, reducers, and store
- The core API (
createSlice,configureStore,createAsyncThunk) - Real-world examples and usage patterns
- Best practices for scalable Redux applications
π§© What Is Redux Toolkit?
Redux Toolkit is the official, recommended way to write Redux logic. It wraps Redux APIs with simplified utilities like:
| Utility | Purpose |
|---|---|
configureStore() | Simplified store setup with DevTools |
createSlice() | Creates reducer + actions in one step |
createAsyncThunk() | Handles async actions (API calls) |
createEntityAdapter() | Normalize and manage collections |
βοΈ 1. Installing Redux Toolkit
npm install @reduxjs/toolkit react-redux
π§ 2. Create a Slice with createSlice
A slice contains:
- A name
- Initial state
- Reducer functions
- Auto-generated action creators
β counterSlice.js
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => {
state.value++; // β
safe mutation via Immer
},
decrement: (state) => {
state.value--;
},
reset: (state) => {
state.value = 0;
},
},
});
export const { increment, decrement, reset } = counterSlice.actions;
export default counterSlice.reducer;
πͺ 3. Configure Store with configureStore
β store.js
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
π¦ Automatically sets up Redux DevTools + middleware
π 4. Use Redux in React Components
β Wrap App with Provider
import { Provider } from 'react-redux';
import { store } from './store';
<Provider store={store}>
<App />
</Provider>
β
Access State with useSelector
import { useSelector } from 'react-redux';
const count = useSelector((state) => state.counter.value);
β
Dispatch Actions with useDispatch
import { useDispatch } from 'react-redux';
import { increment } from './counterSlice';
const dispatch = useDispatch();
<button onClick={() => dispatch(increment())}>+</button>;
π 5. Handling Async Logic with createAsyncThunk
import { createAsyncThunk } from '@reduxjs/toolkit';
export const fetchUser = createAsyncThunk('user/fetchUser', async (id) => {
const res = await fetch(`/api/user/${id}`);
return await res.json();
});
β
Use with extraReducers:
const userSlice = createSlice({
name: 'user',
initialState: { loading: false, data: null },
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchUser.pending, (state) => {
state.loading = true;
})
.addCase(fetchUser.fulfilled, (state, action) => {
state.loading = false;
state.data = action.payload;
});
},
});
β
Clean async handling
β
Works great with loaders, error states
π Redux Toolkit vs Classic Redux
| Feature | Classic Redux | Redux Toolkit |
|---|---|---|
| Store Setup | Manual | configureStore() with DevTools |
| Actions | Manually defined | Auto-generated with createSlice() |
| Reducers | Handwritten switch | Declarative and scoped |
| Async | Redux Thunk or Sagas | createAsyncThunk built-in |
| Immutability | Manual | Built-in with Immer |
| Boilerplate | High | π½ Minimal |
π Best Practices
β
Use one slice per feature/domain
β
Name slices and actions consistently
β
Use createAsyncThunk for all async logic
β
Use selectors to isolate state access
β
Avoid putting local UI state in Redux unless needed globally
π Summary β Recap & Next Steps
Redux Toolkit offers a modern, clean, and scalable approach to global state management in React. It simplifies the traditional Redux workflow while retaining its power.
π Key Takeaways:
- Use
createSlice()to define state + reducers + actions - Use
configureStore()for fast setup with DevTools - Use
createAsyncThunk()for API calls and loading states - Reduces boilerplate and improves readability
- Recommended by the Redux team as the standard in 2025
βοΈ Real-World Relevance:
Used in apps like Notion, GitHub Projects, and enterprise dashboards to manage complex UI and data state in a scalable way.
β FAQ Section
β Is Redux Toolkit a replacement for Redux?
β
It’s built on top of Redux and is now the official recommended way to use it.
β Can I still use regular Redux with Toolkit?
β
Yes. Toolkit is compatible with classic Redux but removes the need for manual setup.
β What makes Redux Toolkit better?
β
Built-in DevTools, simplified slices, Immer-based reducers, async helpers, and less boilerplate.
β Should I use Toolkit for small apps?
β
Yes! Redux Toolkit is simple enough for small apps and powerful enough for large ones.
β Is createAsyncThunk better than redux-thunk?
β
Itβs more structured, typed, and easier to maintain in larger applications.
Share Now :
