Open In App

What are Action’s creators in React Redux?

In React Redux, action creators are functions that create and return action objects. An action object is a plain JavaScript object that describes a change that should be made to the application’s state. Action creators help organize and centralize the logic for creating these action objects.

Action Creators:

Action’s creators in React Redux:

Example : Below are the example of Action’s creators in React Redux.



npm install react-redux
npm install redux




// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import App from './App';
import todoReducer from './reducers/todoReducer';
 
const store = createStore(todoReducer);
 
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);




// App.js
import React from "react";
import "./App.css";
import TodoList from "./components/TodoList";
 
const App = () => {
  return (
   <>
     <TodoList/>
   </>
  );
};
 
export default App;




// src/constants/TodoActionTypes.js
 
export const ADD_TODO = 'ADD_TODO';
export const REMOVE_TODO = 'REMOVE_TODO';




// src/reducers/todoReducer.js
import { ADD_TODO, REMOVE_TODO }
    from "../constants/TodoActionTypes";
     
const initialState = {
    todos: []
};
 
const todoReducer = (state = initialState, action) => {
    switch (action.type) {
        case ADD_TODO:
            return {
                ...state,
                todos: [...state.todos,
                { id: Date.now(), text: action.payload.text }]
            };
        case REMOVE_TODO:
            return {
                ...state,
                todos: state.todos.filter
                    (todo => todo.id !== action.payload.id)
            };
        default:
            return state;
    }
};
 
export default todoReducer;




// src/actions/todoActions.js
 
import { ADD_TODO, REMOVE_TODO }
    from "../constants/TodoActionTypes";
 
export const addTodo = (text) => ({
    type: ADD_TODO,
    payload: { text }
});
 
export const removeTodo = (id) => ({
    type: REMOVE_TODO,
    payload: { id }
});




// src/components/TodoList.js
import React from 'react';
import { useDispatch, useSelector }
    from 'react-redux';
import { addTodo, removeTodo }
    from '../actions/todoActions';
import './TodoList.css';
 
const TodoList = () => {
    const dispatch = useDispatch();
    const todos = useSelector(state => state.todos);
 
    const handleAddTodo = () => {
        const text = prompt('Enter todo:');
        if (text) {
            dispatch(addTodo(text));
        }
    };
 
    const handleRemoveTodo = (id) => {
        dispatch(removeTodo(id));
    };
 
    return (
        <div className="container">
            <div className="todo-list">
                <h1>To-Do List</h1>
                <button onClick={handleAddTodo}>
                    Add Todo
                </button>
                <ul>
                    {todos.map(todo => (
                        <li key={todo.id}>
                            {todo.text}
                            <button onClick={() =>
                                handleRemoveTodo(todo.id)}>
                                Remove
                            </button>
                        </li>
                    ))}
                </ul>
            </div>
        </div>
    );
};
 
export default TodoList;




// src/store.js
import { createStore } from 'redux';
import todoReducer from './reducers/todoReducer';
 
const store = createStore(todoReducer);
 
export default store;




/* TodoList.css */
.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }
   
  .todo-list {
    max-width: 400px;
    width: 100%;
  }
   
  button {
    margin-top: 5px;
    background-color: blueviolet;
    color: white;
    padding: 5px;
    border: 2px solid grey;
    border-radius: 5px;
     
  }

Start your application using the following command.

npm start

Output:



Output


Article Tags :