javascript - 如何使用 setState 更新 state.item[1]?

标签 javascript reactjs state

我正在创建一个应用程序,用户可以在其中设计自己的表单。例如。指定应包含的字段名称和其他列的详细信息。

该组件可用作 JSFiddle here .

我的初始状态是这样的:

var DynamicForm = React.createClass({
  getInitialState: function() {
   var items = {};
   items[1] = { name: 'field 1', populate_at: 'web_start',
                same_as: 'customer_name',
                autocomplete_from: 'customer_name', title: '' };
   items[2] = { name: 'field 2', populate_at: 'web_end',
                same_as: 'user_name', 
                    autocomplete_from: 'user_name', title: '' };

     return { items };
   },

  render: function() {
     var _this = this;
     return (
       <div>
         { Object.keys(this.state.items).map(function (key) {
           var item = _this.state.items[key];
           return (
             <div>
               <PopulateAtCheckboxes this={this}
                 checked={item.populate_at} id={key} 
                   populate_at={data.populate_at} />
            </div>
            );
        }, this)}
        <button onClick={this.newFieldEntry}>Create a new field</button>
        <button onClick={this.saveAndContinue}>Save and Continue</button>
      </div>
    );
  }

我想在用户更改任何值时更新状态,但我很难定位到正确的对象:

var PopulateAtCheckboxes = React.createClass({
  handleChange: function (e) {
     item = this.state.items[1];
     item.name = 'newName';
     items[1] = item;
     this.setState({items: items});
  },
  render: function() {
    var populateAtCheckbox = this.props.populate_at.map(function(value) {
      return (
        <label for={value}>
          <input type="radio" name={'populate_at'+this.props.id} value={value}
            onChange={this.handleChange} checked={this.props.checked == value}
            ref="populate-at"/>
          {value}
        </label>
      );
    }, this);
    return (
      <div className="populate-at-checkboxes">
        {populateAtCheckbox}
      </div>
    );
  }
});

我应该如何制作 this.setState 以使其更新 items[1].name

最佳答案

以下是不使用辅助库的方法:

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});
},

如果需要,您可以将第 2 步和第 3 步结合起来:

let item = {
    ...items[1],
    name: 'newName'
}

或者你可以在一行中完成整个事情:

this.setState(({items}) => ({
    items: [
        ...items.slice(0,1),
        {
            ...items[1],
            name: 'newName',
        },
        ...items.slice(2)
    ]
}));

注意:我将 items 设为一个数组。 OP使用了一个对象。但是,概念是相同的。


您可以在终端/控制台中看到正在发生的事情:

❯ node
> items = [{name:'foo'},{name:'bar'},{name:'baz'}]
[ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ]
> clone = [...items]
[ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ]
> item1 = {...clone[1]}
{ name: 'bar' }
> item1.name = 'bacon'
'bacon'
> clone[1] = item1
{ name: 'bacon' }
> clone
[ { name: 'foo' }, { name: 'bacon' }, { name: 'baz' } ]
> items
[ { name: 'foo' }, { name: 'bar' }, { name: 'baz' } ] // good! we didn't mutate `items`
> items === clone
false // these are different objects
> items[0] === clone[0]
true // we don't need to clone items 0 and 2 because we're not mutating them (efficiency gains!)
> items[1] === clone[1]
false // this guy we copied

关于javascript - 如何使用 setState 更新 state.item[1]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29537299/

相关文章:

javascript - 使用 redux-form 验证检索错误

javascript - 在 Redux 状态下存储具有原型(prototype)函数的对象

reactjs - this.props.location.state 在 React Link Router 中未定义

javascript - FlowType 中可能有不兼容的别名?

javascript - 在 AngularJS 中打开新窗口或新选项卡时停止、启动和销毁间隔

javascript - 如何与多个用户一起运行 Protractor 套件

php - 日期选择器显示日期,例如 2012 年 2 月 9 日星期四

javascript - 为什么我们要在没有依赖数组的情况下使用 useEffect?

javascript - 如何在 React Native State 中添加或减去 Moment JS 值?

dart - 如何在 StatelessWidget 中更改 StatefulWidget 的状态?