KarunakarPatel

Building a Todo App Using Redux Toolkit Slice in React.js: A Comprehensive Guide

Learn how to create a powerful Todo application using Redux Toolkit Slice in React.js. Follow this step-by-step guide, complete with code examples and explanations, to master Redux integration in your React projects.

build-todo-app-using-redux-toolkit

Introduction

Welcome to our comprehensive guide on building a Todo application using Redux Toolkit Slice in React.js. Redux is a powerful state management library for JavaScript applications, and Redux Toolkit provides utilities to streamline the Redux development process. In this tutorial, we'll explore how to leverage Redux Toolkit Slice to manage the state of a Todo application in a React.js environment.

Prerequisites

Before we dive into building our Todo app, make sure you have the following prerequisites installed:

  • Node.js and npm (or yarn) installed on your machine
  • Basic understanding of React.js concepts
  • Familiarity with Redux and Redux Toolkit

Now that we're all set, let's begin by setting up our project.

Setting Up the Project

First, let's create a new React.js project using Create React App. Open your terminal and run the following command:

npx create-react-app todo-app-redux

Once the project is created, navigate into the project directory:

cd todo-app-redux

Next, let's install Redux Toolkit and react-redux:

npm install @reduxjs/toolkit react-redux

or with yarn:

yarn add @reduxjs/toolkit react-redux

With the project set up, let's move on to creating our Redux store.

Creating the Redux Store

In Redux, the store holds the global state of the application. Redux Toolkit simplifies store creation using the configureStore function. Create a new file named store.js in the src directory and add the following code:

import { configureStore } from "@reduxjs/toolkit";
import todoReducer from "./features/todo/todoSlice";

export default configureStore({
  reducer: {
    todos: todoReducer,
  },
});

Here, we import configureStore from Redux Toolkit and define our rootReducer, which combines all our slice reducers. In this case, we only have one reducer for managing Todo state.

Defining the Todo Slice

Now, let's define our Todo slice. A slice is a collection of Redux reducer logic and actions for a specific feature. Create a new file named todoSlice.js in the src/features/todo directory and add the following code:

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

const initialState = {
  todos: [],
};

const todoSlice = createSlice({
  name: "todos",
  initialState,
  reducers: {
    addTodo: (state, action) => {
      state.todos.push(action.payload);
    },
    toggleTodo: (state, action) => {
      const { id } = action.payload;
      const todo = state.todos.find((todo) => todo.id === id);
      if (todo) {
        todo.completed = !todo.completed;
      }
    },
    // Add other reducers as needed
  },
});

export const { addTodo, toggleTodo } = todoSlice.actions;
export default todoSlice.reducer;

In this slice, we define the initial state with an empty array of todos and create reducers for adding and toggling todos.

Integrating Redux with React Components

Now that we have our Redux store and slice set up, let's integrate it with our React components.

Creating the Todo Form Component

Create a new file named TodoForm.js in the src/components directory and add the following code:

import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { addTodo } from "../features/todo/todoSlice";

const TodoForm = () => {
  const [text, setText] = useState("");
  const dispatch = useDispatch();

  const handleChange = (e) => {
    setText(e.target.value);
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    if (text.trim()) {
      dispatch(addTodo({ text, completed: false }));
      setText("");
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <input type="text" placeholder="Enter your todo" value={text} onChange={handleChange} />
      <button type="submit">Add Todo</button>
    </form>
  );
};

export default TodoForm;

Here, we define a functional component TodoForm that captures user input and dispatches the addTodo action when the form is submitted.

Displaying Todos

Create a new file named TodoList.js in the src/components directory and add the following code:

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { toggleTodo } from "../features/todo/todoSlice";

const TodoList = () => {
  const todos = useSelector((state) => state.todos.todos);
  const dispatch = useDispatch();

  const handleToggle = (id) => {
    dispatch(toggleTodo({ id }));
  };

  return (
    <ul>
      {todos.map((todo) => (
        <li
          key={todo.id}
          onClick={() => handleToggle(todo.id)}
          style={{ textDecoration: todo.completed ? "line-through" : "none" }}
        >
          {todo.text}
        </li>
      ))}
    </ul>
  );
};

export default TodoList;

Here, we define a functional component TodoList that renders a list of todos. We use useSelector to access the todos from the Redux store and useDispatch to dispatch the toggleTodo action when a todo is clicked.

Putting It All Together

Now, let's integrate our components into the main App component. Replace the contents of App.js with the following code:

import React from "react";
import "./App.css";
import TodoForm from "./components/TodoForm";
import TodoList from "./components/TodoList";

function App() {
  return (
    <div className="App">
      <h1>Todo App</h1>
      <TodoForm />
      <TodoList />
    </div>
  );
}

export default App;

Conclusion

In this tutorial, we've learned how to build a Todo application using Redux Toolkit Slice in React.js. We started by setting up our project, creating a Redux store, defining a Todo slice with reducers, and integrating Redux with our React components. By following this guide, you now have a solid foundation for managing state in your React applications using Redux Toolkit.

FAQs

How does Redux Toolkit simplify Redux development?

Redux Toolkit provides utilities like configureStore and createSlice to streamline Redux development, reducing boilerplate code and making it easier to write and maintain Redux logic.

Can I use Redux Toolkit with existing Redux projects?

Yes, Redux Toolkit is designed to be compatible with existing Redux projects. You can gradually migrate your Redux codebase to Redux Toolkit for improved developer experience and performance.

What are the benefits of using Redux for state management?

Redux offers a centralized state management solution, making it easier to manage and debug complex application states. It also provides predictable state updates through the use of reducers

and immutability.

Is Redux Toolkit suitable for small projects?

While Redux Toolkit is optimized for large-scale applications, it can also be used in smaller projects to benefit from its simplified API and improved performance.

How does Redux Toolkit handle asynchronous actions?

Redux Toolkit provides middleware like redux-thunk or redux-saga for handling asynchronous actions. These middleware allow you to dispatch asynchronous actions and handle side effects in your Redux logic.

Can I use Redux Toolkit with other UI libraries besides React.js?

Yes, Redux Toolkit can be used with other UI libraries or frameworks like Angular or Vue.js. As long as the library supports JavaScript, Redux Toolkit can be integrated seamlessly.

What are some alternatives to Redux Toolkit for state management in React.js?

Some alternatives to Redux Toolkit for state management in React.js include Context API, MobX, and Zustand. Each of these libraries offers different approaches to managing state in React applications.

I hope you find this guide helpful in building your Todo application with Redux Toolkit Slice in React.js! If you have any further questions, feel free to ask.

If you love this blog post, please share it on
Recent Articles