繁体   English   中英

子组件如何在React React-Native中进行通信?

[英]How can child components communicate in React React-Native?

我怎样才能从test2()访问test1() ,反之亦然? 我已经知道我可以通过ref_to_test1ref_to_test2从父级访问每个。 但是子组件直接相互调用函数呢?

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')
);

最好的方法是使用某种全球事件系统。 有很多简单的库可以完成此操作,或者您可以轻松地自己滚动。

暂无
暂无

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

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