Categories
JavaScript Answers

How to delete an item from Redux state with JavaScript?

Spread the love

To delete an item from Redux state with JavaScript, we can use the array filter method.

For instance, we write

export const commentList = (state, action) => {
  switch (action.type) {
    //...
    case "DELETE_COMMENT":
      return state.filter(({ id }) => id !== action.data);
    //...
  }
};

to call state.filter to return an array without the object with the id property equal the action.data property.

The returned array will be the new value of the state.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *