在完成调度调用之前,setState在reactjs中被调用

时间:2018-12-21 10:43:29

标签: javascript reactjs ecmascript-6 redux

接收到数据后,我正在使用redux调度方法从firebase提取数据,我正在使用条件来计算总量以及在另一个循环中设置所有用户详细信息的逻辑。

在完成调度调用之前,setstate会得到设置。

historyCollection.find({"date":{$gte:new Date("2018-12-20 13:47:57.461Z")}, $or:[{"source":2}, {"source":3}]}).limit(4).toArray(function (err2, result2) {
    // Do Stuff
});

在完成此调度之前,已调用setState:

this.props.dispatch(fetchDuesChitties()).then((datas) => {
  console.log('all Chitties Resp', datas);
  Object.keys(datas).map((key) => {
    if (datas[key].chittieUsers) {
      let chittieUsers = datas[key].chittieUsers;
      Object.keys(chittieUsers).map((userKey, index) => {
        if (chittieUsers[userKey].userId === userId) {
          let totalAmount = 0;
          let payments = 0;
          let auctionsCompleted = datas[key].auctionsCompleted;
          let chitId = datas[key].chitId;
          let paymentDetails = this.props.chits[chitId].paymentDetails;

          for (var i = 0; i < auctionsCompleted; i++) {
            totalAmount += parseInt(paymentDetails[i].dueAmount);
          }

          this.props.dispatch(fetchPaymentsByUserId(userKey, auctionsCompleted)).then((data) => {
            userTicketNumber[key].amountPaid = data;
          });

          userTicketNumber[key] = {
            name: userName,
            chitId: datas[key].chitId,
            chittieId: key,
            chitName: datas[key].chitName,
            chittieName: datas[key].chittyName,
            auctionsCompleted: datas[key].auctionsCompleted,
            userId: userKey,
            totalAmount: totalAmount
          };
        }
        console.log('index is', index+1);
        if (Object.keys(chittieUsers).length === index + 1) {
          // this.getUsersDuePayment(userTicketNumber);
          this.setState({ userFilter: userTicketNumber });
        }
      });
    }
  });
});

Redux通话:

this.props.dispatch(fetchPaymentsByUserId(userKey, auctionsCompleted)).then((data) => {
            userTicketNumber[key].amountPaid = data;
          });

0 个答案:

没有答案