stockroom icon indicating copy to clipboard operation
stockroom copied to clipboard

Can't delete nested object from store

Open btzr-io opened this issue 5 years ago • 2 comments

When used with unistore is impossible to delete / remove a nested object or property. See the following example ( This code only works using unistore without stockroom )

// Store
const state = {
  collections: {
     items: {
        a: { ... },
        b: { ... },
        c: { ... },
     }
    // ...
  }
 // ...
}

// Action
removeItem({ collections }, id) {
      let items = collections.items;
      // Remove item
      if (items[id]) {
        const { [id]: removed, ...rest } = items;
        items = rest;
      }
    return { collections: { ...collections, items } };
},

Unistore only example (working demo)

Edit Unistore Demo

btzr-io avatar Mar 07 '19 03:03 btzr-io

Is there any workaround on this issue ?

btzr-io avatar Mar 07 '19 04:03 btzr-io

@btzr-io the workaround would be to set the property's value to undefined:

// Action
removeItem({ collections }, id) {
      let items = { ...collections.items };
      items[id] = undefined;
    return { collections: { ...collections, items } };
},

developit avatar Mar 08 '19 12:03 developit