简体   繁体   English

交流两个reactJS组件

[英]Communicate two reactJS components

Hi I have this two components that does not have a parent-child relation: 嗨,我有两个没有父子关系的组件:

var Track = React.createClass({
  rawMarkup: function() {
  var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
  return { __html: rawMarkup };
},

render: function() {
  return (
    <li className = 'right_menu_li valign-wrapper ten-margin-bot'>
      <a style={{cursor: 'pointer'}}>
        <i className="tiny material-icons blue-gray base">playlist_add</i>
        {this.props.name + " "}
      </a>    
    </li>
  );
 }
});

and

var ItemList = React.createClass({
  render: function() {
  var createItem = function(item) {
    return <li key={item.id}>{item.text}</li>;
  };
    return <ul>{this.props.items.map(createItem)}</ul>;
 }
});

So I want to know how to create new "li" items in ItemList component when a user clicks on an element in Track component, passing the item properties 因此,我想知道当用户单击Track组件中的元素并传递项目属性时,如何在ItemList组件中创建新的“ li”项目

regards 问候

You basically have two options. 您基本上有两个选择。

1) you can have state controlled by a component that is a parent component to both of your components. 1)您可以通过组件控制状态,该组件是两个组件的父组件。 You can then pass methods down to child components so that they can communicate events / state changes with this master component. 然后,您可以将方法传递给子组件,以便它们可以与此主组件通信事件/状态更改。

2) You can use a flux library. 2)您可以使用助焊剂库。 Currently the most popular flux library among the react community is redux . 当前,在react社区中最流行的flux库是redux Flux enables you to communicate changes in state / events to stores which then emit the change in state to other components which are listening to the stores. Flux使您可以将状态/事件的更改传达给商店,然后将状态更改发送给正在监听商店的其他组件。

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

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