繁体   English   中英

如何在react中更新父级父组件的状态?

[英]How to update state of parent's parent component in react?

我在评论框上玩fb的教程。 让我们想象一下,我添加了删除评论的功能,所以我会在评论组件中有这样的东西 -

onClick={this.handleCommentDelete}

如何触发commentBox父组件状态的更改而不在commentList组件中传播回调然后再发表注释组件?

<commentBox>
  <commentList>
    <comment>

我正在使用PubSub模式在组件之间进行通信。 在commentBox中:

componentDidMount: function() {
    PubSub.subscribe("COMMENT_DELETED", function( msg, data ){
        console.log( data ); //logs "commentId"
    });
}

在handleCommentDelete中:

PubSub.publish("COMMENT_DELETED", {commentId: commentId});

对于PubSub我正在使用这个库(当然还有其他的): https ://www.npmjs.com/package/pubsub-js。

暂无
暂无

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

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