简体   繁体   中英

How can child components communicate in React React-Native?

How can I get access to test1() from test2() or vice versa? I already know I have access to each from the parent thru ref_to_test1 and ref_to_test2 . But what about the children components calling functions to each other directly?

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

The best way to accomplish this would be to use a global events system of some kind. There are plenty of simple libraries out there to accomplish this, or you could roll your own easily enough.

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