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

Mongodb ReactJS:从子组件中的更改重新呈现父组件,mongodb,reactjs,meteor,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()} </P

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

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

<Parent>
  {this.renderChildren()}
</Parent>
renderChildren(){
让children=Mongo.Collection.find().fetch();//对象数组
返回children.map((child)=>(
));
}
{this.renderChildren()}
。。。在子组件中

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>
  )
}
toggleStatus(){
//更改集合的状态
Meteor.call('changeStatus',this.props.child.\u id,this.props.child.status);
}
render(){
返回(
改变状态
)
}
问题:每次子对象更改状态时,都会重新呈现整个父对象。有道理


如何仅重新渲染子对象而不是父对象?

有两种方法可以解决此问题:

  • 通过将子组件设为类并使其扩展React.component,为其提供子组件状态。我个人会选择第二种
  • 将toggleStatus从父级传递给子级,并使用它在子级中设置状态

  • 关于父组件和子组件,这可能会让您更好地了解如何传递它。

    好的,下面是我尝试的内容。在父组件中,在extends
    TrackerReact(component){
    下,我有我的函数:
    handleStatus(e){Meteor.call('toggleStatus',e.categoryName,e._id,e.status);}
    然后在我的
    collectionFunction(){return posts.map((post)=>);}
    中,然后在我的子组件中
    返回()
    问题是,所有函数在呈现…想法时立即启动?好的,我通过将子组件更改为
    this.handleStatus(post)}/>);}来阻止它立即启动但父级正在重新渲染。。。使用状态会有所不同吗?好吧,我想我知道,也许,该怎么做:在我的子组件
    类中,子扩展组件{constructor(props){super(props);this.state=this.props.child}
    好的,这会将每个子组件的状态设置为从父组件传递给它的对象…在子组件内部,我只能反映子组件的状态。当我单击按钮时,它会更改子组件的状态并更新数据库…这听起来对吗?好的,算出了。这是TrackerReact在做的重新阅读…grrrrr…太棒了,很高兴你明白了,我希望我能帮助你!