2017-03-16 49 views
0

设置在父组件的默认类扩展组件:ReactJS:从改变家长组件重新呈现在孩子

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

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

  1. 通过使一类,并让它延长React.Component给孩子组件状态。我个人会做方案二
  2. 通行证toggleStatus孩子从父和使用,在孩子设置状态。

I answered this question关于父母和孩子的组件,可能会给你一个更好的想法如何将其传递下来。

+0

好的,这是我的尝试。在父组件中,在扩展'TrackerReact(Component){''我有我的功能:'handleStatus(e){Meteor.call('toggleStatus',e.categoryName,e._id,e.status); }' 然后,在我的'collectionFunction(){返回posts.map((柱)=>());}'然后,在我的孩子的组件'返回()'问题,所有的功能,只要它呈现...思想火灾? – Daltron

+0

好吧,我立即通过将子组件更改为' this.handleStatus(post)} />));}来停止它的发射,但父母正在重新呈现再次...将使用状态有所作为? – Daltron

+0

好吧,我想我明白,也许,该怎么做:在我的子组件{class Child}中扩展组件{constructor(props){super(props); this.state = this.props.child}'好吧,这会将每个子组件的状态设置为从父对象传递给它的对象......在孩子内部,我可以只反映孩子的状态。当我点击按钮时,它应该改变孩子的状态并更新数据库......听起来是对的吗? – Daltron

相关问题