Skip to content Skip to sidebar Skip to footer

Remove An Item From Local Storage In Reactjs

Is this the right code to remove an item from localStorage: onDelete(name) { console.log('ProductList.onDelete: ', name); let {products} = this.state; produ

Solution 1:

To remove items from your local storage, you simply have to run:

localStorage.removeItem("name of the item")

Solution 2:

    let items =JSON.parse(localStorage.getItem("item"));
    items = items.filter((item) => item.id !== id);
    localStorage.setItem("item", JSON.stringify(items));
    if (items.length === 0) {
      localStorage.removeItem("item");
    }

Solution 3:

@Prometheus is correct.

Since, you have asked for ReactJS, I hope that following fiddle helps you: https://jsfiddle.net/ghoshnirmalya/eqb9vych/5/.

addItem = () => localStorage.setItem("name", this.state.name)

removeItem = () => localStorage.removeItem("name")

I have added a whole example in ReactJS.


Post a Comment for "Remove An Item From Local Storage In Reactjs"