How to handle more action using redux ?
Last Updated :
08 Mar, 2022
In Redux, actions are the JavaScript object which has information. Having multiple actions will need multiple action creators. These actions should be unique in redux. Especially, the type of action must be defined properly to identify that.
We will take the example of to-do list actions. There are actions we will define and handle:
- Add todo item
- Delete todo item
Let’s create an application to demonstrate how to handle more action using redux.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app example
Step 2: After creating your project folder i.e. example, move to it using the following command:
cd example
Step 3: Module installation, now from the root directory of your project in the terminal, run the following command
npm install redux
npm install react-redux
Step 4: In src folder, make two new folder components and redux. In components add two jsx files named addTodo.jsx and listTodo.jsx. In redux folder, create a folder name actions and add index.js file to it. Then in redux folder create a folder name reducer and add two files named index.js and todoreducer.js. And then in redux folder add store.js file.
Project Structure: It will look like this.

Project Structure
Example: Let’s make a simple todo application with help of redux. Write down the following codes in respective files.
index.js
import React from 'react' ;
import ReactDOM from 'react-dom' ;
import './index.css' ;
import { Provider } from 'react-redux' ;
import App from './App' ;
import store from '../src/redux/store' ;
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById( 'root' ),
);
|
App.js
import AddTodo from './components/addTodo' ;
import ListTodo from './components/listTodo' ;
function App() {
return (
<div className= "App" >
<h1>Todo List</h1>
<AddTodo></AddTodo>
<ListTodo></ListTodo>
</div>
);
}
export default App;
|
addTodo.jsx
import React, { useState } from 'react' ;
import { useDispatch } from 'react-redux' ;
import { addtodoitem } from '../redux/actions' ;
const AddTodo = () => {
const [todo, setTodo] = useState( '' );
const dispatch = useDispatch();
return (
<div>
<h2>Enter the todo</h2>
<label htmlFor= "todoitem" />
<input
type= "text"
id= "todoitem"
value={todo}
onChange={(e) => {
setTodo(e.target.value);
}}
/>
<button
onClick={() => {
dispatch(
addtodoitem({
title: todo,
done: false ,
}),
);
setTodo( '' );
}}>
Add todo
</button>
</div>
);
};
export default AddTodo;
|
listTodo.jsx
import React from 'react' ;
import { useSelector, useDispatch } from 'react-redux' ;
import { deltodoitem } from '../redux/actions' ;
const ListTodo = () => {
const todos = useSelector((state) => state.todo);
const dispatch = useDispatch();
return (
<div>
{todos.map((todo, index) => (
<div>
<p style={{ display: 'inline' , margin: '10px' }}>
{todo.title}
</p>
<button
onClick={() => {
dispatch(deltodoitem(index));
}}>
Delete
</button>
</div>
))}
</div>
);
};
export default ListTodo;
|
redux/index.js
const addtodoitem = (item) => {
return {
type: "ADD_TODO" ,
payload : item,
};
};
const deltodoitem = (id) => {
return {
type: "DELETE_TODO" ,
payload : id,
};
};
export {addtodoitem,deltodoitem};
|
reducer/todoreducer.js
const todoreducer = (state = [], action) => {
if (action.type === 'ADD_TODO' ) {
return [...state, action.payload];
}
if (action.type === 'DELETE_TODO' ) {
return state.filter((item, index) => index !== action.payload);
}
return state;
};
export default todoreducer;
|
reducer/index.js
import todoreducer from "./todoreducer" ;
import { combineReducers } from "redux" ;
const rootReducer = combineReducers({
todo : todoreducer,
})
export default rootReducer;
|
redux/store.js
import rootReducer from "./reducer" ;
import { createStore } from "redux" ;
const store = createStore(rootReducer);
export default store;
|
We have defined the multiple actions of index.js file of the actions folder. They are using todoreducer file to perform the changes in the state of data of redux. This way we handle multiple actions in redux.
Step to run the application: Use the following command to run the application:
npm start
Output: Here, when we type in the input box and click on “Add todo” button ADD_TODO action is called and when we click on the “Delete” button DELETE_TODO is called. This way is able to handle two or more actions in redux.

Similar Reads
How to Handle Forms in Redux Applications?
Handling forms in Redux applications involves managing form data in the Redux store and synchronizing it with the UI. By centralizing the form state in the Redux store, you can easily manage form data, handle form submissions, and maintain consistency across components. We will discuss a different a
5 min read
How to Handle Errors in React Redux applications?
To handle errors in Redux applications, use try-catch blocks in your asynchronous action creators to catch errors from API calls or other async operations. Dispatch actions to update the Redux state with error information, which can then be displayed to the user in the UI using components like error
4 min read
How do you handle real-time updates in Redux applications?
Handling real-time updates is essential for modern web applications to provide users with dynamic and interactive experiences. In Redux applications, managing real-time updates efficiently involves implementing strategies to synchronize the application state with changes occurring on the server in r
5 min read
How to dispatch asynchronous actions using Redux Thunk?
Asynchronous actions are very important in web development, particularly in tasks such as fetching data from API. Redux Thunk is a middleware using which you can write action creators that return a function instead of an object. This function can perform asynchronous operations and dispatch actions
3 min read
How to handle data fetching in React-Redux Applications ?
Data fetching in React-Redux applications is a common requirement to retrieve data from APIs or other sources and use it within your components. This article explores various approaches to handling data fetching in React-Redux applications, providing examples and explanations for each approach. Tabl
5 min read
Action's Payload in React Redux
In the realm of React Redux, understanding how actions and their payloads work is fundamental to efficient state management. Actions serve as messengers that convey information from your application to the Redux store, triggering state updates. Among the key components of actions is the payload, whi
6 min read
How to handle server-side errors in Redux applications ?
It is essential to deal with server-side errors while building reliable Redux applications. This ensures a seamless user experience, even if problems occur during server requests. This guide will explain the effective management of server errors in the Redux architecture through Redux Thunk middlewa
3 min read
How to use React Context with React-Redux ?
React context with React-Redux is a popular state management library for React applications. Using React context with React-Redux is a powerful way to provide the Redux store to components deep within your component tree without manually passing it down through props. PrerequisitesNode.js and NPMRea
3 min read
Create a Simple Form Application using React Redux
In this article, we make a simple project of form application built using React and Redux. The main purpose of the application is to collect user information through a form consisting of fields for name, email, message and submit button. It's built with React for the user interface and Redux for man
3 min read
How to handle app state without Redux ?
To handle app state without redux in a react application we will the the React JS hooks. For functional component hooks enable access and and manage data inside the states. PrerequisitesNPM & Node.jsReact JSReact useState Hook ApproachTo handle the app state without Redux we will maintain the st
5 min read