Delete an item from Redux state Delete an item from Redux state reactjs reactjs

Delete an item from Redux state


Just filter the comments:

case 'DELETE_COMMENT':  const commentId = action.data;  return state.filter(comment => comment.id !== commentId);

This way you won't mutate the original state array, but return a new array without the element, which had the id commentId.

To be more concise:

case 'DELETE_COMMENT':  return state.filter(({ id }) => id !== action.data);


You can use Object.assign(target, ...sources) and spread all the items that don't match the action id

case "REMOVE_ITEM": {  return Object.assign({}, state, {    items: [...state.items.filter(item => item.id !== action.id)],  });}


You can use Try this approach.

case "REMOVE_ITEM":   return {  ...state,    comment: [state.comments.filter(comment => comment.id !== action.id)]  }