2016-08-14 57 views
0

我如何才能从test2()访问test1(),反之亦然?我已经知道我可以通过ref_to_test1ref_to_test2访问父母中的每一个。但是,儿童组件直接调用对方的功能呢?子组件如何在React React-Native中进行通信?

var CommentBox = React.createClass({ 

    render: function() { 
    console.log(this) 
    return (
    <div className="commentBox"> 
     <h1>Comments</h1> 
     <CommentList ref={'ref_to_test1'}/> 
     <CommentForm ref={'ref_to_test2'}/> 
     </div> 

    ); 
    } 
}); 
var CommentList = React.createClass({ 
test1:function(){}, 
    render: function() { 
    console.log(this) 
    return (
     <div className="commentList"> 
     Hello, world! I am a CommentList.  
     </div> 
    ); 
    } 
}); 

var CommentForm = React.createClass({ 
test2:function(){}, 
    render: function() { 
    return (
     <div className="commentForm"> 
     Hello, world! I am a CommentForm. 
     </div> 
    ); 
    } 
}); 
ReactDOM.render(
    <CommentBox />, 
    document.getElementById('content') 
); 

回答

0

完成此操作的最佳方法是使用某种全局事件系统。有很多简单的图书馆可以完成这个任务,或者你可以很容易地推出自己的图书馆。

相关问题