ReactJS:父组件从子组中的更改重新呈现

时间:2017-03-16 22:57:42

标签: mongodb reactjs meteor

在父组件中设置为默认类扩展组件:

renderChildren() {
  let children = Mongo.Collection.find().fetch(); // array of objects
  return children.map((child) => (
    <Child key={child._id} child={child} />
  ));
}

<Parent>
  {this.renderChildren()}
</Parent>

...在子组件中

toggleStatus() {
  //changes the status on collection
  Meteor.call('changeStatus', this.props.child._id, this.props.child.status); 
}
render() {
  return (
    <span onClick={this.toggleStatus.bind(this)}>Change Status</span>
  )
}

问题:每次孩子改变状态时,都会重新呈现整个父母。有道理......

我怎样才能重新渲染孩子,而不是父母?

1 个答案:

答案 0 :(得分:1)

我能想到有两种方法可以解决这个问题:

  1. 通过使子类成为一个类并使其扩展为React.Component来赋予子组件状态。我个人会选择两个
  2. 将toggleStatus传递给来自父级的子级,并使用它来设置子级中的状态。
  3. I answered this question关于父和子组件可能会让您更好地了解如何传递它。

相关问题