美文网首页
2018-03-19 利用await/async对react.j

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

作者: Allan要做活神仙 | 来源:发表于2018-03-19 12:28 被阅读25次
  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里用合适。

相关文章

网友评论

      本文标题:2018-03-19 利用await/async对react.j

      本文链接:https://www.haomeiwen.com/subject/msouqftx.html