How to update object in React state

后端 未结 5 1371
野的像风
野的像风 2021-02-18 22:30

I have an indexed list of users in the JS object (not array). It\'s part of the React state.

{
    1: { id: 1, name: \"John\" }
    2: { id: 2, name         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 23:21

    This is what i would do

    • add: var newUsers = _.extend({}, users, { 4: { id: 4, ... } })
    • remove: var newUsers = _.extend({}, users) then delete newUsers['2']
    • change: var newUsers = _.extend({}, users) then newUsers['2'].name = 'Peter'

提交回复
热议问题