[英]How can child components communicate in React React-Native?
How can I get access to test1()
from test2()
or vice versa? 我怎样才能从
test2()
访问test1()
,反之亦然? I already know I have access to each from the parent thru ref_to_test1
and ref_to_test2
. 我已经知道我可以通过
ref_to_test1
和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.
有很多简单的库可以完成此操作,或者您可以轻松地自己滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.