简体   繁体   English

React.js在子级修改道具

[英]React.js Modifying Props at the Child Level

Normally my props are strings. 通常我的道具是弦乐器。 As expected, "modifying" strings at the child level does not modify the state directly because strings are immutable and only references will change. 不出所料,在子级上“修改”字符串不会直接修改状态,因为字符串是不可变的,只有引用会改变。 However, I recently passed an object as a prop. 但是,我最近通过了一个物体作为道具。 Modifying the members of that object are passed up directly to the state. 修改该对象的成员将直接传递到状态。 I understand why this happens, I'm just wondering if this is an anti-pattern in React.js? 我知道为什么会这样,我只是想知道这是否是React.js中的反模式?

For example: 例如:

/** @jsx React.DOM */
var ChildComponent = React.createClass({
  click: function(e) {
    this.props.xyz.subObject = "abc";
    this.props.onClick();
  },
  render: function() {
    return(
      <div onClick={this.click}>Click Me to Update Parent State</div>
    );
  }
});

var ParentComponent = React.createClass({
  getInitialState: function() { return { xyz: { subObject: "123" } }; },
  childClick: function() {
    this.setState(this.state);
  },
  render: function() {
    return(
      <div>
      <ChildComponent onClick={this.childClick} xyz={this.state.xyz} />
      <pre>AppState: <br /><br />{ JSON.stringify(this.state, 0, 2) }</pre>
      </div>
    );
  }
});

React.renderComponent(<ParentComponent />, document.body);

Why would anybody want to do this? 为什么有人要这样做? This particular discovery came while prototyping a GUI that has rows, columns, and items. 在对具有行,列和项的GUI进行原型制作时,就发现了这一特殊发现。 Rows can be ordered by dragging them. 可以通过拖动来对行进行排序。 Rows contain columns that can be ordered or moved into other rows, again by dragging. 行包含可重新排序或通过拖动可排序或移动到其他行的列。 Columns have items that can be dragged between columns in the same or different rows. 列中的项目可以在相同或不同行中的列之间拖动。 On top of all this, items are components too and can update themselves, eventually passing that update back up to the root state. 最重要的是,项目也是组件,可以自行更新,最终将更新传递回根状态。

Changing prop object members at the item level, and firing a chained event to call setState , worked as well as calling the chained this.props.onEvent(rowId, colId, itemId, newItemProps) . 在项目级别更改prop对象成员,并触发一个链式事件以调用setState ,以及调用链式this.props.onEvent(rowId, colId, itemId, newItemProps) Which is good, because now it no longer matters that the item is part of a column, or that the column is in a row, or that the row is part of the root component. 这样做很好,因为现在不再重要的是项目是列的一部分,还是列在行中,或者行是根组件的一部分。 The only other solution was to add a bunch of knowledge about the item model to the root component so it could try to find and replace the correct params. 唯一的其他解决方案是将有关项目模型的大量知识添加到根组件,以便它可以尝试查找和替换正确的参数。

This is why you should use Flux stores. 这就是为什么您应该使用Flux商店。 Stores can be observed by parents. 父母可以观察商店。 Children modify the stores through actions. 孩子们通过动作来修改商店。 The stores notify the parents to get new state. 商店通知父母获得新的状态。 The parents rerender the children with new props. 父母用新的道具给孩子们送礼物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM