【问题标题】:React Reflux Stores: Difference between calling trigger() with parameters vs. without parametersReact Reflux Stores:带参数调用 trigger() 与不带参数的区别
【发布时间】:2016-02-25 01:46:20
【问题描述】:

我有一个带有 Reflux 存储的 React 应用程序,其中一些组件侦听 trigger(),然后调用 Reflux 存储 getters 以检索其更新的状态。即

var Store = Reflux.createStore({
  init: function() {
    this.filterList = [];
    ... // listening to actions
  }

  onNewFilterItemAction: function(item) {
    this.filterList.push(item);
    this.trigger(...);
  }
});

使用参数调用trigger() 与不调用有什么区别?即:

  onNewFilterItemAction: function(item) {
    this.filterList.push(item);
    this.trigger(this.filterList);
  }

对比

  onNewFilterItemAction: function(item) {
    this.filterList.push(item);
    this.trigger();
  }

【问题讨论】:

标签: reactjs store state flux refluxjs


【解决方案1】:

您可以选择性地更新。

  componentDidMount = () => { this.unsubscribe = BasicStore.listen(this.storeDidChange); }
  componentWillUnmount = () => { this.unsubscribe(); }
  storeDidChange = (id) => {
    switch (id) {
      case 'data1': this.setState({Data1: BasicStore.getData1()}); break;
      case 'data2': this.setState({Data2: BasicStore.getData2()}); break;
      case 'data3': this.setState({Data3: BasicStore.getData3()}); break;
      default: this.setState(getState());
    }
  }

【讨论】:

  • 鼓励这种模式吗?获取部分商店更新不是危险且有问题吗?
  • 这会有什么危险和错误。它是具有多个属性的单个对象。
  • 我的意思是随着存储变得越来越复杂,动作和存储状态突变交织在一起,仅在存储状态的一部分上调用 getter() 可能意味着不会检索其他更新的存储状态。当然,对于更简单的代码库,这可以很容易地避免。
  • 你可以同时拥有它。当您只需要一次更新时传递一个参数。跳过参数并全部更新。
  • 对。那我应该把它混合起来吗?只坚持一个?在什么情况下我应该使用哪个?非常感谢!
猜你喜欢
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多