Redux vs Redux Toolkit : Which is Better?
Redux is a popular state management library for JavaScript applications, especially in React. However, managing state in traditional Redux requires boilerplate code, which led to the introduction of Redux Toolkit (RTK).
In this article, we will compare Redux and Redux Toolkit, explaining their differences, advantages, and which one you should use in modern applications.
1. What is Redux?
Redux is a predictable state container for JavaScript applications. It follows a unidirectional data flow where the state is managed in a single store and updated through actions and reducers.
Key Concepts of Redux
- Store: The central place where the application’s state is stored.
- Actions: Plain JavaScript objects that describe an event (e.g.,
"USER_LOGGED_IN"
). - Reducers: Functions that take the current state and an action and return a new state.
- Dispatch: The method used to send actions to reducers.
- Selectors: Functions to retrieve specific data from the Redux store.
Problems with Traditional Redux
While Redux provides a structured way to manage state, it has some downsides:
- Too much boilerplate code – Developers have to manually write action types, action creators, and reducers.
- Complex configuration – Setting up Redux with middleware (like
redux-thunk
orredux-saga
) requires additional effort. - Immutability handling – Developers must manually ensure state immutability, often using
spread
operators (...
).
Because of these challenges, Redux Toolkit (RTK) was introduced.
2. What is Redux Toolkit?
Redux Toolkit (RTK) is the official, recommended way to use Redux. It simplifies Redux development by reducing boilerplate code, handling immutable updates efficiently, and integrating middleware by default.
Key Features of Redux Toolkit
- Simplified syntax with
createSlice()
. - Built-in Thunk support for async operations.
- Automatic immutability handling with Immer.js.
- Better developer experience with
configureStore()
.
3. Key Differences Between Redux and Redux Toolkit
Feature | Traditional Redux | Redux Toolkit (RTK) |
---|---|---|
Boilerplate Code | High (manual actions, reducers) | Low (auto-generates actions, reducers) |
State Management | Manual updates using spread (...state ) | Uses Immer.js for easy immutable updates |
Middleware Setup | Manual (redux-thunk required) | Pre-configured with Thunk |
Async Handling | Complex (redux-thunk , redux-saga ) | Built-in with createAsyncThunk() |
Store Configuration | Manually set up combineReducers | Uses configureStore() for simpler setup |
Performance | Standard performance | Optimized with automatic batching |
Now, let’s dive into the implementation differences.
4. Code Comparison: Redux vs Redux Toolkit
Traditional Redux Example
Here’s how we define a counter feature using classic Redux:
Step 1: Define Action Types
const INCREMENT = "INCREMENT";
const DECREMENT = "DECREMENT";
Step 2: Create Action Creators
const increment = () => ({ type: INCREMENT });
const decrement = () => ({ type: DECREMENT });
Step 3: Define the Reducer
const initialState = { count: 0 };
const counterReducer = (state = initialState, action) => {
switch (action.type) {
case INCREMENT:
return { ...state, count: state.count + 1 };
case DECREMENT:
return { ...state, count: state.count - 1 };
default:
return state;
}
};
Step 4: Create the Store
import { createStore } from "redux";
const store = createStore(counterReducer);
export default store;
This requires multiple steps to set up actions, reducers, and the store.
Redux Toolkit Implementation
With RTK, the same counter logic can be written in fewer lines using createSlice()
:
Step 1: Define Slice (Actions + Reducers in One Place)
import { createSlice, configureStore } from "@reduxjs/toolkit";
const counterSlice = createSlice({
name: "counter",
initialState: { count: 0 },
reducers: {
increment: (state) => { state.count += 1; },
decrement: (state) => { state.count -= 1; },
}
});
export const { increment, decrement } = counterSlice.actions;
const store = configureStore({ reducer: counterSlice.reducer });
export default store;
✅ Why is this better?
- No need to define action types manually (RTK does it for you).
- Reducers modify state directly (RTK uses Immer.js under the hood).
- Store setup is simpler with
configureStore()
.
5. Handling Asynchronous Actions (API Calls)
Traditional Redux (With Thunk)
const fetchData = () => {
return async (dispatch) => {
dispatch({ type: "FETCH_REQUEST" });
try {
const response = await fetch("https://api.example.com/data");
const data = await response.json();
dispatch({ type: "FETCH_SUCCESS", payload: data });
} catch (error) {
dispatch({ type: "FETCH_ERROR", payload: error.message });
}
};
};
Redux Toolkit (With createAsyncThunk)
import { createAsyncThunk, createSlice, configureStore } from "@reduxjs/toolkit";
export const fetchData = createAsyncThunk("data/fetch", async () => {
const response = await fetch("https://api.example.com/data");
return response.json();
});
const dataSlice = createSlice({
name: "data",
initialState: { items: [], status: "idle" },
extraReducers: (builder) => {
builder
.addCase(fetchData.pending, (state) => { state.status = "loading"; })
.addCase(fetchData.fulfilled, (state, action) => {
state.status = "succeeded";
state.items = action.payload;
})
.addCase(fetchData.rejected, (state) => { state.status = "failed"; });
}
});
const store = configureStore({ reducer: dataSlice.reducer });
export default store;
✅ Why is RTK Better?
- Handles async logic in a structured way with
createAsyncThunk()
. - Reduces need for extra action types like
FETCH_REQUEST
,FETCH_SUCCESS
,FETCH_ERROR
. - Cleaner and more readable code.
6. Should You Use Redux or Redux Toolkit?
Use Redux If:
✔ You have an existing project built with traditional Redux.
✔ You prefer manually handling reducers, actions, and middleware.
Use Redux Toolkit If:
✔ You are starting a new project (RTK is now the recommended approach).
✔ You want less boilerplate and better performance.
✔ You need built-in async handling for API calls.
✔ You prefer easier store configuration.
7. Conclusion: Redux Toolkit is the Future
While Redux is still widely used, Redux Toolkit (RTK) is the recommended approach for modern applications. It simplifies state management, reduces boilerplate code, and improves performance with built-in tools.
If you are working on a new React project, using Redux Toolkit is the best choice. 🚀