Reputation: 15
//i need to add objects into a array using reducer for contact book
//reducer
const addContact = (contacts = [], action) => {
let contactsArr = [{}];
if (action.type = "ADD_CONTACT") {
return [...contactsArr, action.payload];
}
return contacts;
};
actions
export const addContactRed = contact => {
return {
type: "ADD_CONTACT",
payload: contact
};
};
{
type:"ADD_CONTACT",
payload:{name:"xyz",phonenum:10101001}
}
{
type:"ADD_CONTACT",
payload:{name:"abc",phonenum:0101001}
}
//after dispatching two actions final array i want is
//contactsArr
[
{name:"xyz",phonenum:10101001},
{name:"abc",phonenum:0101001}
]
Upvotes: 0
Views: 42
Reputation: 1560
You don't have ton init let contactsArr = [{}]; It will reset the store value in your reducer. Just use the contact store variable
const addContact = (contacts = [], action) => {
// if (action.type = "ADD_CONTACT") {
if (action.type === "ADD_CONTACT") {
return [...contacts, action.payload];
}
return contacts;
};
Upvotes: 1