简体   繁体   中英

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

I'm playing around with fb's tutorial on comment box. Let's imagine I've added functionality to delete comments, so I will have something like this in Comment component -

onClick={this.handleCommentDelete}

How do I trigger change of the state of the commentBox parent component without propagating callback throughout commentList component and then to comment component?

<commentBox>
  <commentList>
    <comment>

I am using PubSub pattern to communicate between components. In commentBox:

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

In handleCommentDelete:

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

For PubSub I am using this library (but there are others of course): https://www.npmjs.com/package/pubsub-js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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