【发布时间】:2019-02-07 03:42:15
【问题描述】:
此函数从我正在使用的 API 返回令牌数据。
// api.js
var token="";
const tokenAl= {
tokenner: function(){
fetch("myurl/api/token", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json-patch+json"
},
body: JSON.stringify({
// here is not important
})
})
.then(response => response.json())
.then(responseJson => {
token= responseJson.result.token;
console.warn(token) // it's ok. I got the token.
})
.catch(error => console.warn(error));
// this.props.navigation.navigate("Customer"); // Customer sayfasına gidiş.
},
}
export default tokenner;
一切正常。现在,如何在 Login-Form.js 文件中使用这个令牌值?
【问题讨论】:
-
您需要在组件内部使用
state,并在您提取令牌的then方法回调中调用组件的状态更新器方法。 -
我试过了,但我做不到。你能为我写信吗?
标签: javascript reactjs react-native react-router