【问题标题】:Why is prevState argument null in calculateState method in Flux?为什么 Flux 的 calculateState 方法中的 prevState 参数为空?
【发布时间】:2018-01-09 07:43:06
【问题描述】:

我在构造函数this.state = {}; 中设置了状态,但calculateState 方法中的prevState 参数为空。我应该在哪里设置容器的初始状态?

class QuestionnairesContainer extends Component {
  static getStores() {
    return [QuestionnairesStore];
  }

  static calculateState(prevState) {
    return {
      questionnairesList: QuestionnairesStore.getState().questionnairesList,
      pagingObject: prevState.pagingObject
    };
  }

  constructor(props) {
    super(props);
    this.state = {
      pagingObject: someData
    };
  }

  render() {
    return (
      <section>
      </section>
    );
  }
}

export default Container.create(QuestionnairesContainer);

【问题讨论】:

    标签: reactjs flux reactjs-flux


    【解决方案1】:

    我在通量GitHub 上找到了解决方案。

      static calculateState(prevState) {
        const init = prevState ? {} : {
          pagingObject: someData,
        };
        return {
          ...init,
          questionnairesList: QuestionnairesStore.getState().questionnairesList
        };
      }
      constructor(props) {
          super(props);
      }
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2011-11-22
      • 2019-08-24
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 2020-07-11
      • 2019-05-16
      • 2020-10-27
      相关资源
      最近更新 更多