DEV Community

Discussion on: How To Immutably Update An Array In TypeScript

Collapse
 
shelob9 profile image
Josh Pollock

That example is massively over simplified, I can see why it's confusing. In my case it does need to be decoupled from the component as the state is shared in multiple components. Also, state management is shared with parts of the page that are not created with React.

Yes, what you show works for adding an item. It would still need an updater method.

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

as the state is shared in multiple components

Would it be possible to lift the state up to the common ancestor of all those components and then drill it down using props? If not, you may want to reach for Redux.

The updater method is setItems.

Thread Thread
 
shelob9 profile image
Josh Pollock

Yes. That would be a good startegy.

What I like about writing state management decoupled from React is I can make that call later. If I have an updateItem, removeItem, and deleteItem, and they have tests, I can choose to use React.useState() or something Redux-like depending on my need.