How Redux Toolkit simplifies Redux code in React application ?
Last Updated :
23 Jul, 2025
Redux Toolkit is a powerful library designed to simplify the complexities of managing application state with Redux in React applications. At its core, Redux Toolkit provides developers with a set of utilities and abstractions that significantly reduce boilerplate code and streamline common Redux tasks.
This means that developers can focus more on building features and less on writing repetitive Redux setup code. With Redux Toolkit, you can define your Redux state, actions, and reducers in a single file using the createSlice function, which saves time and keeps your code organized. Additionally, setting up the Redux store is streamlined with the configureStore function, which automatically configures common middleware like Redux Thunk.
Benefits of Redux Toolkit
- Reduced Boilerplate code: Redux Toolkit significantly reduces the amount of boilerplate code needed to set up and manage Redux in your application. This means less time spent writing repetitive code and more time focusing on building features.
- Simplified Syntax: It provides a simplified syntax for defining Redux logic, making it easier to understand and maintain. For example, the createSlice function allows you to define slices of state along with their associated reducers and action creators in a concise and organized manner.
- Streamlined Setup: Redux Toolkit streamlines the process of setting up the Redux store by automatically configuring common middleware like Redux Thunk and Redux DevTools Extension. This eliminates the need for manual setup and ensures that your Redux store is set up with best practices in mind.
- Improved Developer Experience: By abstracting away many of the low-level details of Redux, Redux Toolkit improves the developer experience. It provides intuitive abstractions and utilities that simplify common Redux tasks, making it easier for developers to work with Redux in their React applications.
- Enhanced Performance: Redux Toolkit includes performance optimizations such as memoized selectors through the createSelector utility. These optimizations help improve the performance of your React application by preventing unnecessary re-renders and optimizing the way data is accessed from the Redux store.
Approach to Create Redux Toolkit simplify Redux code in a React application:
We'll use Redux Toolkit to manage the state of our Todo list. The state will include an array of Todo items, each with an ID, text, and completion status. We'll create Redux actions to add, toggle completion, and remove Todo items. The Todo list will be displayed in a React component, and users will be able to interact with it by adding new items, marking items as complete, and removing items.
- Centralized State Management :The application utilizes Redux as a central store to manage the to-do list state.
- ConfigureStore : We configure the Redux store using configureStore from Redux Toolkit, combining the Todo reducer to centrally manage the Todo state accessible by any component.
- Todo Component : Making Todo components utilizing useSelector to access state and useDispatch to dispatch actions.
- Integration : Integrate Todo component, wrapping with Provider, passing Redux store to ensure access across the application
Steps to Create Application
Step 1 : Make a project name directory and navigate to it using this command.
mkdir todo
cd todo
Step 2 : Create React App using the following command.
npx create-react-app .
Step 3 : Install required dependencies using the following command.
npm install react-redux @reduxjs/toolkit
Step 4 : Create Redux slice (todoSlice) to manage Todo list state.
Step 5 : Configure Redux store (store) and combine the Todo reducer.
Step 6 : Create Todo components (TodoList, TodoInput) to display and interact with Todo items.
Step 7 : Integrate Todo components into the index file and wrapped it by Provider tag.
Project Structure:
Todo Project Structure
Updated dependencies in package.json file - The updated dependencies in package.json file will look like.
"dependencies": {
"@reduxjs/toolkit": "^2.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
},
Example: Below is an example of Redux Toolkit simplify Redux code in React.
CSS
/* style.css */
#header {
display: flex;
align-items: center;
}
#index_wrapper {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
#add_btn {
padding: 10px 20px;
background-color: rgb(19, 186, 19);
color: white;
border-radius: 10px;
}
input {
padding: 10px 20px;
border-radius: 10px;
margin-left: 20px;
}
.remove_btn {
background-color: rgb(19, 186, 19);
margin: 0px 5px;
padding: 5px;
border: 1px solid rgb(19, 186, 19);
border-radius: 5px;
cursor: pointer;
}
.todo_text {
cursor: pointer;
margin: 10px 0px;
}
JavaScript
// store.js
// todo slice
import {
createSlice,
configureStore
} from '@reduxjs/toolkit';
const todoSlice = createSlice({
name: 'todos',
initialState: [],
reducers: {
addTodo: (state, action) => {
let newTodo = {
id: Date.now(),
text: action.payload,
completed: false,
};
state.push(newTodo);
newTodo = null;
},
toggleTodo: (state, action) => {
let todo = state.find(
todo => todo.id === action.payload);
if (todo) {
todo.completed = !todo.completed;
}
todo = null
},
removeTodo: (state, action) => {
return state.filter(
todo => todo.id !== action.payload);
},
},
});
export const { addTodo, toggleTodo, removeTodo } = todoSlice.actions;
// store
export default configureStore({
reducer: {
todos: todoSlice.reducer
},
});
JavaScript
// TodoComp.js
import React, {
useState
} from "react";
import {
useSelector,
useDispatch
} from "react-redux";
import {
addTodo,
toggleTodo,
removeTodo
} from "./store";
import "./style.css";
export const TodoList = () => {
const todos = useSelector((state) => state.todos);
const dispatch = useDispatch();
const handleToggleTodo = (id) => {
dispatch(toggleTodo(id));
};
const handleRemoveTodo = (id) => {
dispatch(removeTodo(id));
};
return (
<div>
<h2>Todo List</h2>
<ul>
{todos.map((todo) => (
<li className="todo_text" key={todo.id}>
<span
style={{
textDecoration:
todo.completed ? "line-through" : "none",
}}
onClick={() => handleToggleTodo(todo.id)}
>
{todo.text}
</span>
<button
className="remove_btn"
onClick={() => handleRemoveTodo(todo.id)}
>
Remove
</button>
</li>
))}
</ul>
</div>
);
};
export const TodoInput = () => {
const [todo, setTodo] = useState("");
const dispatch = useDispatch();
const addHandle = () => {
if (!todo.trim()) return null;
dispatch(addTodo(todo));
setTodo("");
};
return (
<div id="header">
<img
src="https://media.geeksforgeeks.org/gfg-gg-logo.svg"
alt="gfg_logo"
/>
<input
type="text"
value={todo}
onChange={(e) => setTodo(e.target.value)}
/>
<button id="add_btn" onClick={addHandle}>
Add
</button>
</div>
);
};
JavaScript
// index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { TodoInput, TodoList } from './TodoComp';
import store from './store';
import { Provider } from 'react-redux';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<div id='index_wrapper'>
<TodoInput />
<TodoList />
</div>
</Provider>
);
Output:
Todo Final Output
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
8 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDom is a core react package that provides methods to interact with the Document Object Model or DOM. This package allows developers to access and modify the DOM. Let's see in brief what is the need to have the package. Table of ContentWhat is ReactDOM ?How to use ReactDOM ?Why ReactDOM is used
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
6 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects