Skip to main content

Command Palette

Search for a command to run...

Redux Toolkit for beginners.

Developers should understand React basics like hooks, components, react life-cycle, named and default export, and basic JavaScript.

Updated
5 min read
Redux Toolkit for beginners.

What is Redux Toolkit, and Why Does It Exist?

To truly appreciate Redux Toolkit, we need to dive into the world of Redux, understand how it works, and how things were before the toolkit came along!

But, let’s be honest — handling global states with just the useState() hook in React can get tedious. We all know about prop drilling, where you have to pass state from one component to another repeatedly. It's okay for small projects, but when you scale up, and your app has tons of components, it turns into a nightmare! Passing state from the Main ComponentTodo List ComponentTodo Item Component becomes exhausting!

Now, imagine you have 100 components instead of just 2. You’re passing state through layers of components that don’t even need it. Yikes!

Enter Redux — the Hero!

Redux swoops in to solve this problem. It allows you to manage global state without having to pass props through every single layer. However, the old way of using Redux was complex! You had to write a lot of boilerplate code: defining actions, creating reducers, setting up the store, and manually wiring everything together.

What is Redux?

Redux is an incredible state management library! It helps you manage your application's global state efficiently.

So, Why Redux Toolkit?

To make life easier! 🎉 The Redux Toolkit simplifies everything. It reduces the boilerplate and handles most of the setup automatically. You still define the store and reducer, but Redux Toolkit takes care of the actions and configures the store for you, so you can focus on building awesome features rather than setup!

In short, Redux Toolkit is a developer's best friend for cutting down on the unnecessary steps, making it easier and faster to use Redux in your projects!


Let’s Dive into the Code by Building a Simple Counter-application

Ready to get your hands dirty with some code? Let’s build a simple counter application that will showcase the magic of Redux Toolkit. You’ll see firsthand how easy it is to manage a global state in your React app. Let’s get started!

Setting Up the Project

First, we need to set up the project. Open your terminal and run the following command to create a new React app:

npx create-react-app redux-counter-app
cd redux-counter-app
npm install @reduxjs/toolkit react-redux

Boom! You now have a fresh React project with Redux Toolkit ready to go. 🎉

Creating the Counter Slice

Now, let’s create the counter slice—this is where we define our state and actions. Create a new file called counterSlice.js:

import { createSlice } from '@reduxjs/toolkit';

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

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;

This slice manages the counter’s state and provides actions to increment or decrement the value. How cool is that?!

Connecting the Slice to the Store

Next, we need to set up the Redux store. In your store.js file, import your counterSlice and configure the store:

import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counterSlice';

export const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
});

Now, the store is ready and connected to our slice! Time to make it interactive. 😎

Writing the Component to Interact with the Store

In your App.js, let’s create a simple UI for the counter. Here’s how you can dispatch actions and access the state using useSelector and useDispatch from react-redux:

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

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

  return (
    <div>
      <h1>Counter: {count}</h1>
      <button onClick={() => dispatch(increment())}>+</button>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}

export default App;

And just like that, you’ve created a fully functional counter app! 🚀

Explanation and Walkthrough of the Example

We’ve used Redux Toolkit to simplify Redux setup and made use of createSlice() to generate reducers and actions all in one go. We connected the slice to the store and interacted with it via React components using useSelector to read the state and useDispatch to trigger actions. Simple, right?

One more example with full code Todo App. You have to implement Update feature by your own.

Best Practices

When working with Redux, it’s important to follow best practices to keep your app scalable and maintainable:

  1. Keep state normalized – Store data in a way that's easy to access and modify.

  2. Avoid large, monolithic reducers – Split them up into smaller slices.

  3. Use createSlice() and createAsyncThunk() for cleaner and more readable code.

Code Organization

Organizing your Redux code is key to maintaining a scalable project. Separate your slices, components, and utilities into different folders. Structure your code by features rather than types (e.g., keep everything related to the counter in one place).

Debugging with Redux DevTools

Redux DevTools is an absolute game-changer for debugging. You can inspect every action dispatched and track how your state changes over time. Install the extension, and you’ll never want to debug without it again!

Common Mistakes and How to Avoid Them

  1. Misusing createSlice and createAsyncThunk: Always define actions that are specific to your slice and handle async actions with createAsyncThunk().

  2. Inefficient State Management: Avoid duplicating data in your state. Normalize your state to keep it efficient.

  3. Debugging Errors: Make use of redux-logger or Redux DevTools to trace any errors in your Redux flow.

Additional Resources

Here are some fantastic resources to keep leveling up your Redux skills:

  • Official Redux Toolkit Documentation – Your go-to resource for everything Redux!

  • Community Tutorials – Check out Reddit and GitHub for tons of tutorials and open-source projects.

  • Example Projects for Practice – Try building a todo app, e-commerce store, or even a social media dashboard using Redux Toolkit.

Conclusion

Congratulations! 🎉 You’ve just built a full counter application using Redux Toolkit and learned about some best practices along the way. With Redux Toolkit, you can focus more on building amazing features and less on boilerplate code. Now it’s your turn to explore more complex projects! Happy coding!

More from this blog