Updating the array object in React state using immutability helper

后端 未结 2 621
渐次进展
渐次进展 2020-12-25 14:11

I am updating an object within an array in React state using immutability helper.

The object I want to modify is nested:



        
2条回答
  •  离开以前
    2020-12-25 14:36

    Im importing update from immutability helper here :)

    this.state = {
      a: {
        b: [{ c: '', d: ''}, ...]
      }
    } 
    
    
    this.setState((prevState, props) => update(prevState, {
        a: {
            b: {
                $apply: b => b.map((item, ii) => {
                    if(ii !== n) return item;
                    return {
                        ...item,
                        c: 'new value'
                    }
                })
            }
        }
    }))
    

提交回复
热议问题