0

This is a more of a can it ever be done even with a parser like babel or anything type of question which i believe is allowed in SU. All just to make my code cleaner for Reducer and State in general; what I want to do is:

return {
   ...state,
   myObj: {
       ...state.myObj,
      items: [
        ...state.myObj.items,
        [index]: newValue, // do something like this
      ]
   }
}

Anyone know how we can do this cleanly? I dont want to use a slice() or just do newState.myObj.items[index] = newValue as that messy and difficult to read as per other SU answers. Maybe I'm the only one that OCD about clean reducer states, without using a library. Anyone have come across this pattern or how one may achieve it? Id be happy to install a bebel plugin but cant seem to find one.

Thanks

Kivylius
  • 6,357
  • 11
  • 44
  • 71
  • 1
    Since you mentioned reducer, I'll link you to the immutable update patterns for [Inserting and Removing Items in Arrays with Redux](https://redux.js.org/usage/structuring-reducers/immutable-update-patterns#inserting-and-removing-items-in-arrays) – Tom Mar 11 '22 at 19:08
  • @RameshReddy ah im not the only one then, wonder if babel could plugin could transform what we have to object assign way. – Kivylius Mar 11 '22 at 19:10
  • @Tom yea thats the current way im doing it, just wonder if `babeljs` could transform that, so i dont have to write it every time. – Kivylius Mar 11 '22 at 19:12
  • Ive requested an a change, can be updated if people want this in javascript eventually. https://es.discourse.group/t/update-a-specific-index-of-an-array-when-using-spread-operator/1249 – Kivylius Mar 11 '22 at 19:47

0 Answers0