简体   繁体   English

如何停止执行React代码直到axios请求完成?

[英]how to stop executing react code until axios request gets completed?

I'm using React JS. 我正在使用React JS。

At First, I need to fetch data from database using REST API and with this results, I'm setting some variables which further I'm passing to another class. 首先,我需要使用REST API从数据库中获取数据,并在此结果的基础上设置一些变量,这些变量将进一步传递给另一个类。 Now I want to make a synchronous request through axios so that only after setting data control should go next. 现在,我想通过axios发出同步请求,以便仅在设置数据控制之后才继续。 In short, I want to stop rendering my code till my variables will set to data. 简而言之,我想停止渲染代码,直到将变量设置为数据为止。 How to achieve this through axios? 如何通过axios实现这一目标?

In React there is no way in doing this. 在React中,无法做到这一点。 This is intentional. 这是故意的。 You should think about how to handle this. 您应该考虑如何处理。

The obvious choice here is to show a loading indicator or any other form of output to render while you waiting for the response. 显而易见,这里的选择是显示一个加载指示器或任何其他形式的输出,以供您等待响应时使用。

After you receive your response you can set the state in order to rerender your component and show the desired output. 收到响应后,您可以设置状态以重新渲染组件并显示所需的输出。

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

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