Search code examples
reactjsfluxreactjs-flux

Why is prevState argument null in calculateState method in Flux?


I set state in constructor this.state = {};, but prevState argument in calculateState method is null. Where should I set initial state of container?

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);

Solution

  • I have found a solution on flux GitHub.

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