防止数据发返回时组件不存在(unmount)

1,  componentDidMount() {

    this.mounted = true;

  }

2,   componentWillUnmount() {

    this.mounted = false;

  }

3,fetchThisWeekData() {

    const params = {};

    params.flag = 0;

    Request.GET(API.getEmployeeAttendanceData(params), (responseData) => {

      if (!this.mounted) return;

      this.thisWeek = responseData;

      // console.log('thisweek----', this.thisWeek);

      // this.thisWeek = [[2, 2, 6], [3, 6, 2], [1, 0, 4], [0, 4, 6], [3, 6, 0], [0, 0, 0], [4, 4, 4]];

      if (Util.isAndroid) {

        this.setState({

          data: {

            datasets: [{

              yValues: this.thisWeek,

              label: '',

              config: {

                barSpacePercent: 60,

                colors: ['#56e295', '#617fde', '#f9bf13'],

                stackLabels: [`${I18n.t('regularHours')}`, `${I18n.t('overtime')}`, `${I18n.t('leave')}`],

                valueTextColor: 'white',

                valueTextFontSize: 6,

              },

            }],

            xValues: [`${I18n.t('monday')}`, `${I18n.t('tuesday')}`, `${I18n.t('wednesday')}`, `${I18n.t('thurday')}`, `${I18n.t('friday')}`, `${I18n.t('saturday')}`, `${I18n.t('sunday')}`],

          },

          loaded: true,

        });

      }

      if (Util.isIos) {

        this.state.config.dataSets[0].values = this.thisWeek;

        this.setState({

          config: this.state.config,

          loaded: true,

        });

      }

    }, (message) => {

      if (!this.mounted) return;

      Message.showMessage('error', message);

    });

  }

猜你喜欢

转载自222xiaohuan.iteye.com/blog/2322889
今日推荐