简体   繁体   English

我应该如何在React中与我的组件通信?

[英]How should i communicate with my component in React?

i am new player of pure Reactjs , i have a question in my case below : 我是纯Reactjs的新手,以下是我的问题:

My material include navComponent.js, stackComponent.js, nav.js 我的资料包括navComponent.js,stackComponent.js,nav.js

I want the nav.js data is get from stackComponent.js pass to navComponent.js, and then render the DOM in navComponent i want. 我想将nav.js数据从stackComponent.js中获取并传递给navComponent.js,然后在想要的navComponent中呈现DOM。 Because the nav.js data, i need to reuse for another components in the furture. 因为nav.js数据,我需要为将来的其他组件重用。 As the below code, i think i use this wrong react concept. 作为下面的代码,我想我使用这个错误的反应概念。 How should i do in CORRECT React way. 我应该如何以正确的React方式做。

In Header of index.html include 在index.html的标题中包含

<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="react/browser.min.js"></script>

In Body of index.html include 在index.html的主体中包括

<div id="stackWrapper"></div>

In Footer of index.html include 在index.html的页脚中包含

<!-- Data -->
<script src="js/nav.js" type="text/javascript"></script>

<!-- Components -->
<script src="components/navComponent.js" type="text/babel"></script>
<script src="components/stackComponent.js" type="text/babel"></script>

In navComponent.js 在navComponent.js中

window.NotesList = React.createClass({
  render: function() {
      return (
          <nav className="pages-nav">
          {
            React.Children.map(this.props.children, function (child) {
                return <div>{child}</div>;
            })
          }
          </nav>
      );
  }
});

In stackComponent.js 在stackComponent.js中

var StackWrapper = React.createClass({

  render: function() {
      return (
          <div>
              <NotesList>
                  {
                      navItems.map(function(value, key){
                          console.log(key);
                          return <div key={key}>{value.title.name}</div>
                      })
                  }
              </NotesList>
          </div>
      )
  }
});

ReactDOM.render(
  <StackWrapper />,
  document.getElementById('stackWrapper')
);

In nav.js 在nav.js中

var navItems = [
  {
      title: {
          name: 'home',
      },
      data: {
          link: {
              src: 'page-home'
          }
      }
  },
  {
      title: {
          name: 'docu',
      },
      data: {
          link: {
              src: 'page-docu'
          }
      }
  }
]

You could pass navItems as props to stackWrapper: 您可以将navItems作为道具传递给stackWrapper:

In stackComponent.js: 在stackComponent.js中:

ReactDOM.render(
   <StackWrapper navItems={navItems} />,
    document.getElementById('stackWrapper')
 );

In stackComponent.js, You can pass navItems using: 在stackComponent.js中,您可以使用以下命令传递navItems:

<NotesList navItems={this.props.navItems}>..</NoteList>

In navComponent.js, You can collect navItems using: 在navComponent.js中,可以使用以下命令收集navItems:

this.props.navItems

It's not a rule ok. 规则不行。 The better and recommended approach , is using events. 更好的推荐方法是使用事件。 You can dispatch an event from some component and receive the update at any place you need. 您可以从某个组件调度事件,并在所需的任何位置接收更新。

You don't need to implement a entire Flux based application to solve this, you can for example, use the event api of browsers or use a custom dispatcher 您无需实现整个基于Flux的应用程序即可解决此问题,例如,可以使用浏览器的事件api或使用自定义调度程序

In case your components are in same tree, you can update the state and pass state's props as argument to the children components. 如果您的组件位于同一棵树中,则可以更新状态并将状态的prop作为参数传递给子组件。

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

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