简体   繁体   English

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

[英]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_test1ref_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.

相关问题 如何与 React 中的子组件通信? - How to communicate with child components in React? 在React Native中的组件之间进行通信(子级 - >父级) - Communicate Between Components in React Native (child -> parent) 如何在 react-native 样式的组件中使用不透明度? - How can i use opacity in react-native styled components? 父组件和子组件都在 react-native 中同时渲染 - Both the parent and the child components get rendered simultaneously in react-native 分享 React-Native 组件 - Share React-Native components React-Native Real-Time Firebase 如何找到所有子组件(所有引用)? - React-Native Real-Time Firebase How to find all child components(all references)? 如何从共享组件文件夹导入不同的应用程序? 反应 / 反应原生 - How can different apps import from a shared components folder? react / react-native 在子组件和父组件之间进行通信以更改react-native中的Parent的视图内容的正确方法是什么? - What is the right way to communicate between child and parent component to change view content in Parent in react-native? React-Native 对象作为 React 子对象无效 - React-Native Objects are not valid as a React child 我在react-native中获取了带有fetch的JSON,如何将结果发送到其他组件 - I get a JSON with fetch in react-native how can i send the results to other components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM