简体   繁体   English

Reactjs如何从不同的组件更改组件的状态

[英]Reactjs how to change the state of a component from a different component

I have a react component, lets call it as component 1 我有一个react组件,让我们将其称为组件1

define([..., /path/component_2.jsx], function(..., Component2) {
   var Component1 = React.createClass({
      getInitialState: function() {
         return {.......};
      },

      componentDidMount: function() {
          .......
          dates = ....;
          Component2.setState({dates: dates});
      }
       render: function() { return (<div ...></div>) }
   });
}

As you can see, I am calling the Component2.setState in this component. 如您所见,我在此组件中调用Component2.setState。 But I am getting an error like setState is not a function. 但我得到一个错误,如setState不是一个函数。 I tried this with defining a custom function instead of setState function in component 2 and calling this function from component 1, but I am getting the same error, 'is not a function'. 我尝试在组件2中定义自定义函数而不是setState函数,并从组件1调用此函数,但我得到相同的错误,'不是函数'。

And component 2: 和组件2:

define([..., ], function(...) {
   var Component2 = React.createClass({
      getInitialState: function() {
         return {.......};
      },

      render: function() { return (<div ...></div>) }
   });
}

So I guess in reactjs we calls a component only for rendering something (React DOM elements)? 所以我想在reactjs中我们只调用一个组件来渲染某些东西(React DOM元素)? and cannot change the component state? 并且不能改变组件状态?

If so how can I change a state of a component from a different component which is not a child or parent of first? 如果是这样,我如何从不是第一个孩子或父母的不同组件中更改组件的状态?

Components don't publicly expose their state. 组件不公开暴露其状态。 It's also important to remember that the state is scoped to the instance of components, not their definition. 同样重要的是要记住,状态的范围是组件的实例 ,而不是它们的定义。

To communicate between components, you could roll your own event subscription service. 要在组件之间进行通信,您可以推送自己的事件订阅服务。

var events = new EventEmitter();

// inside Component1
events.emit('change-state', newState);

// inside Component2
events.on('change-state', function(state) {
  this.setState(state);
});

However, this will quickly become difficult to manage. 但是,这很快就会变得难以管理。

A more sensible solution is to use Flux . 更明智的解决方案是使用Flux It allows you to explicitly manage the state of your entire application and subscribe to changes in different bits of the state, within your components. 它允许您显式管理整个应用程序的状态,并在组件内订阅状态的不同位的更改。 It's worth trying to wrap your head around it. 值得尝试包围它。

The component that wants to communicate should dispatch an action and this action will be responsible for changing something in the stores, your other component should subscribe to that store and can update its state based on the change. 想要通信的组件应该分派一个动作,这个动作将负责改变商店中的某些东西,你的另一个组件应该订阅那个商店,并且可以根据变化更新它的状态。

You can use a shared state between the two component. 您可以在两个组件之间使用共享状态。 You can build a "mixin" like that 你可以像这样构建一个“mixin”

app.mixins.DateMixin = {
   getInitialState: function () 
      return {
          dates: []
         }
      }
};

and then in you components you can include this mixins using the mixins array 然后在你的组件中,你可以使用mixins数组包含这个mixins

define([..., /path/component_2.jsx], function(..., Component2) {
   var Component1 = React.createClass({

      mixins: [app.mixins.DateMixin],

      getInitialState: function() {
         return {.......};
      },

      componentDidMount: function() {
          .......
          dates = ....;
          this.setState({dates: dates});
      }
       render: function() { return (<div ...></div>) }
   });
}

define([..., ], function(...) {

   mixins: [app.mixins.DateMixin],

   var Component2 = React.createClass({
      getInitialState: function() {
         return {.......};
      },

      render: function() { return (<div ...></div>) }
   });
}

Now your components share the "dates" state and you can change/check it in both of them. 现在,您的组件共享“日期”状态,您可以在两个组件中更改/检查它。 NB: you can also share methods with in the same way by writing into a mixin component. 注意:您也可以通过写入mixin组件以相同的方式共享方法。

Edit: I suggest you to visit this website http://simblestudios.com/blog/development/react-mixins-by-example.html 编辑:我建议你访问这个网站http://simblestudios.com/blog/development/react-mixins-by-example.html

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

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