2018-03-19 利用await/async对react.j

2018-03-19  本文已影响25人  Allan要做活神仙
  async componentDidMount() {
    const getCenterInfo = "/mobile/workbench/getCenterInfo";
    const cacheData = await getCacheData(getCenterInfo);  // 先获取本地存储

    if(cacheData){
      this.setState({ hasWeekly: cacheData.hasWeekly })  // 存在就先使用
    }

    // 调用接口
    this.props.actions.checkPermission().then(res => {
      if (res.success) {
        set("userId", res.result.userId);
        set("currentUserId", res.result.userId);
        this.setState({
          ready: true
        });
          return;
        }
      }
    });
  }

tips:
componentWillMount使用async根本不会阻断React去调用render,shouldComponentUpdate用async返回一个false而依然会被认为返回布尔的true。

async/await这招,还是主要在componentDidMount和componentDidUpdate里用合适。

上一篇下一篇

猜你喜欢

热点阅读