DEV Community

Dibyajyoti
Dibyajyoti

Posted on

State Management in React with Redux Toolkit (Beginner-Friendly Guide)

When you start building React apps, managing state feels simple. A few useState hooks here and there, maybe some props passed down. But as your app grows, things get messy fast. Data starts flowing in too many directions, components become tightly coupled, and debugging turns into a headache.

That’s where state management tools like Redux Toolkit come in.


What is State Management?

State refers to the data your app needs to function. For example:

  • User login status
  • Form inputs
  • API data
  • UI states (like modals, loaders)

Managing this state means deciding:

  • Where it lives
  • Who can access it
  • How it gets updated

Why Not Just Use React State?

React already gives you:

  • useState
  • useContext

These are great for small apps. But in larger apps:

  • Props drilling becomes annoying
  • State gets duplicated
  • Logic becomes scattered

You need a central place to manage everything cleanly.


What is Redux Toolkit?

Redux Toolkit (RTK) is the modern, simplified way to use Redux.

Old Redux had issues:

  • Too much boilerplate
  • Complex setup
  • Confusing patterns

Redux Toolkit fixes that by:

  • Reducing code
  • Providing built-in best practices
  • Making state logic easier to write and maintain

Core Concepts (Simple Breakdown)

1. Store

The store is the global state container.

Think of it like:

A single object that holds all your app’s data


2. Slice

A slice represents a piece of state + its logic.

Example:

  • userSlice
  • cartSlice
  • themeSlice

Each slice includes:

  • Initial state
  • Reducers (functions that update state)

3. Reducers

Reducers define how state changes.

They are just functions:

(state, action) => newState
Enter fullscreen mode Exit fullscreen mode

With Redux Toolkit, you can write simpler code:

state.value += 1
Enter fullscreen mode Exit fullscreen mode

(No need to manually return new objects)


4. Actions

Actions are events that trigger state updates.

Example:

dispatch(increment())
Enter fullscreen mode Exit fullscreen mode

5. Dispatch

Dispatch is how you send actions to the store.


Setting Up Redux Toolkit (Step-by-Step)

1. Install

npm install @reduxjs/toolkit react-redux
Enter fullscreen mode Exit fullscreen mode

2. Create a Slice

// features/counterSlice.js
import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  value: 0,
};

const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
  },
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
Enter fullscreen mode Exit fullscreen mode

3. Create Store

// app/store.js
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "../features/counterSlice";

export const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
});
Enter fullscreen mode Exit fullscreen mode

4. Connect to React App

// main.jsx or index.js
import { Provider } from "react-redux";
import { store } from "./app/store";

<Provider store={store}>
  <App />
</Provider>;
Enter fullscreen mode Exit fullscreen mode

5. Use State in Components

import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "./features/counterSlice";

function Counter() {
  const count = useSelector((state) => state.counter.value);
  const dispatch = useDispatch();

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => dispatch(increment())}>+</button>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Why Redux Toolkit is Worth Learning

1. Centralized State

Everything lives in one place, easier to debug.

2. Predictable Updates

State only changes through reducers.

3. Scalable Architecture

Perfect for large apps.

4. Cleaner Code

Less boilerplate than classic Redux.


When Should You Use Redux Toolkit?

Use it when:

  • Your app has complex state
  • Multiple components need the same data
  • You’re working on medium to large projects

Avoid it if:

  • Your app is very small
  • Simple useState works fine

Common Beginner Mistakes

  • Overusing Redux for small apps
  • Putting everything in one slice
  • Not structuring folders properly
  • Forgetting to normalize large datasets

Pro Tip

Don’t rush into Redux just because it's popular. Learn:

  1. useState
  2. useContext
  3. Then move to Redux Toolkit when needed

Final Thoughts

Redux Toolkit isn’t just a library, it’s a pattern for thinking about state.

Once it clicks, your apps become:

  • Easier to scale
  • Easier to debug
  • Easier to maintain

If you're aiming to build real-world React apps or preparing for jobs, this is a skill that will pay off heavily.

Top comments (0)