简体   繁体   English

如何使React组件等待Redux存储变量被设置?

[英]How to make React component wait for redux store variables to get set?

I'm working on a react app and using redux to manage the store variables. 我正在开发一个反应应用程序,并使用redux来管理商店变量。 The store variables get updated on an API call on the homepage, and are used in api calls in other components as query parameters. 存储变量在主页上的API调用上更新,并在其他组件的api调用中用作查询参数。

I set the store variables in the homepage component like so: 我在主页组件中设置存储变量,如下所示:

API.get("EmployerApiGw", "/employer/" + this.state.shadowAccountId).then(resp => {
      if (resp.items.length > 0) {
        this.setState({
          isValid: true,
          openDialog: false
        });
        this.props.updateAccountid(this.state.shadowAccountId);
        this.props.updateCompanyId(resp.items[0].companyid);
      } else {
        this.setState({
          isValid: false
        });
      }
    });

I'm having an issue where the components get mounted before the store variables are updated in the api call. 我遇到一个问题,即在api调用中更新存储变量之前要挂载组件。 This causes the other api calls to fail because the store variables are null at that point. 这会导致其他api调用失败,因为此时存储变量为null。

API.get("EmployerApiGw", "/employer/" + this.props.accountid + "/billingstatements", {
      queryStringParameters: { companyid: this.props.companyinfo.companyid }
    })

My temporary fix has been to use componentDidUpdate() to call componentDidMount() if the props have changed (I'm passing the store variables as props to the components). 我的临时解决方法是,如果道具已经更改,则使用componentDidUpdate()调用componentDidMount() (我将商店变量作为道具传递给组件)。 This still causes the invalid api calls to run, but re-renders the components once the store variables have updated. 这仍然会导致无效的api调用运行,但是一旦存储变量已更新,就重新渲染组件。

componentDidUpdate(prevProps) {
    if (JSON.stringify(prevProps) !== JSON.stringify(this.props)) {
      this.componentDidMount();
    }
  }

This still causes unnecessary api calls to be made, and clutters up the console. 这仍然会导致不必要的api调用,并使控制台混乱。

I was wondering if there is a way to make components wait until the store variables have finished updating. 我想知道是否有一种方法可以使组件等到存储变量完成更新。

Thank you. 谢谢。

I would put a loading spinner in the child component and have that show by default. 我会在子组件中放置一个加载微调器,并默认显示该内容。 Then I would put an if statement around your second API call so it only gets called if the arguments you need have already been initilised. 然后,我将在第二个API调用周围放置一个if语句,以便仅在所需参数已被初始化时才调用它。 Something like this: 像这样:

if (this.props.accountid && this.props.companyinfo && this.props.companyinfo.companyid) {
  //do api call
}

Then use componentDidUpdate to watch for when the props change. 然后使用componentDidUpdate来查看道具何时更改。 When they are no longer null you can call your API and replace the loading spinner with the rest of the child component you were waiting for API data to render. 当它们不再为null时,您可以调用API并将加载微调框替换为等待API数据呈现的其余子组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM