Can you write a basic action creator with parameters

0 votes
Can i know Can you write a basic action creator with parameters?
23 hours ago in Node-js by Ashutosh
• 27,850 points
12 views

1 answer to this question.

0 votes

Creator with Parameters

Let's say we're building a to-do app and want an action to add a new task.

// actionTypes.js

export const ADD_TODO = 'ADD_TODO';


// actions.js

import { ADD_TODO } from './actionTypes';

// Action creator with parameters

export const addTodo = (id, title) => {

  return {

    type: ADD_TODO,

    payload: {

      id,

      title,

    },

  };

};

Usage Example in a Component:

import { useDispatch } from 'react-redux';

import { addTodo } from './actions';

const TodoInput = () => {

  const dispatch = useDispatch();

  const handleAdd = () => {

    dispatch(addTodo(1, 'Finish project'));

  };

  return <button onClick={handleAdd}>Add Task</button>;

};

answered 17 hours ago by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Can you force a React component to rerender without calling setState?

In functional components, the best way to ...READ MORE

answered Feb 22 in Node-js by Kavya
154 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
134 views
0 votes
1 answer

How does Redux middleware handle async actions?

Redux middleware manages asynchronous actions by intercepting ...READ MORE

answered 17 hours ago in Node-js by anonymous
13 views
0 votes
1 answer

How does put() help in dispatching actions in Sagas?

put() is a Redux-Saga effect that allows ...READ MORE

answered 17 hours ago in Node-js by anonymous
16 views
0 votes
1 answer

How do you write a generator function in Redux-Saga?

In Redux-Saga, generator functions are used to ...READ MORE

answered 17 hours ago in Node-js by anonymous
14 views
0 votes
1 answer

How do you test a generator function in Redux-Saga?

Testing a saga means manually stepping through ...READ MORE

answered 17 hours ago in Node-js by anonymous
14 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP