reactjs - React axios:如何在继续代码之前等待数据
问题描述
所以我必须调用 API 来获取令牌,然后使用响应调用另一个 API 来登录应用程序。
使用同样的代码,render() 要求一个 ';' 在它之后,console.log 在返回令牌之前返回消息......
这是我的代码:
APITOKEN.js:
import axios from 'axios';
import React,{Component} from 'react';
import ReactDOM from "react-dom";
apitoken = async () =>{
this.isLoading = true;
try {
await axios.post('http://192.168.100.60:94/jderest/tokenrequest', {
username: 'JDE',
password: 'JDE'
})
.then(response => {
console.log('returned data from api'+response.data.userInfo.token);
this.userdetails = response.data.userInfo.token;
this.isLoading = false;
})
.catch(error => {
console.log(error);
this.error = error
})
} catch (e) {
console.log('ERROR', e);
this.isLoading = false;
};
};
export default apitoken;
Login.js
const handleLogin = (credentials,setSubmitting) =>{
handleMessage(null);
this.apitoken();
apitoken = async () =>{
await this.props.Apitoken.apitoken();
}
render () {
const { isLoading, userDetails, error } = this.props.Apitoken
return (<View>
{(!!isLoading)?<Text>{userDetails}</Text>:<Text>Loading</Text>}
</View>)
};
console.log('jul222222222');
};
解决方案
推荐阅读
- xml - How to convert an xml file to dictionary in vb using paste special
- javascript - How to change createTextNode value using switch
- macos - Dialogs are displayed as tabs in Android Studio IDE then the IDE becomes unresponsive
- c# - Declare foreign keys using Data Annotation to use ReferentialConstraints collection?
- azure - ARM template fails when creating event hub in multiple regions with multiple auth rules
- linux - Bash script does not wait for user to enter response
- eclipse - Missing artifact in vaadin start project (maven issue)
- python - Output next row from criteria in Python DataFrame
- iis - Reverse proxy server configuration with 2 different servers in iis
- wordpress - 如何在本地主机中添加 .htaccess 文件?意味着什么应该写在文件中?