i want to replace an object inside an array of object using id to find it, the payload has the new object
const initialState = {
allComments: []
};
case LIKE_COMMENT:
let index = state.allComments.findIndex(
value => value._id === payload._id
);
if (index === -1) {
return {
...state,
allComments: [...state.allComments, ...payload]
};
} else {
return {
...state,
allComments: [
(state.allComments[index] = payload),
...state.allComments
]
};
}
them problem that it keep pushing that object without replacing the previous one