📌  相关文章
📜  如何使用 setState 更新 state.item[1]? React - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:22.605000             🧑  作者: Mango

代码示例1
handleChange: function (e) {
    // 1. Make a shallow copy of the items
    let items = [...this.state.items];
    // 2. Make a shallow copy of the item you want to mutate
    let item = {...items[1]};
    // 3. Replace the property you're intested in
    item.name = 'newName';
    // 4. Put it back into our array. N.B. we *are* mutating the array here, but that's why we made a copy first
    items[1] = item;
    // 5. Set the state to our new copy
    this.setState({items});
},