🧠 React State Management
Estimated reading: 4 minutes 46 views

πŸ› οΈ 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:

UtilityPurpose
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

FeatureClassic ReduxRedux Toolkit
Store SetupManualconfigureStore() with DevTools
ActionsManually definedAuto-generated with createSlice()
ReducersHandwritten switchDeclarative and scoped
AsyncRedux Thunk or SagascreateAsyncThunk built-in
ImmutabilityManualBuilt-in with Immer
BoilerplateHighπŸ”½ 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ› οΈ React Redux Toolkit – Simplified Redux

Or Copy Link

CONTENTS
Scroll to Top