[英]react-native dynamically get/set configuration variables (API URL)
My app starts by choosing a country and I want to change my API_URL according to the country data a user selects. 我的应用程序首先选择一个国家/地区,然后我想根据用户选择的国家/地区数据更改我的API_URL。
My country_id is stored in AsyncStorage. 我的country_id存储在AsyncStorage中。 I have tried this but is has not worked.
我已经尝试过了,但是没有用。
function Configure() {
let url = '';
}
Configure.prototype.getApiUrl = function (params = null) {
AsyncStorage.getItem("country").then((value) => {
if(value == 223) {
return "https://www.website.com/usa_api"
}else{
return "https://www.website.com/api"
}
});
}
module.exports = Configure
You have to return the Promise function 您必须返回Promise函数
function Configure() {
let url = '';
}
Configure.prototype.getApiUrl = function (params = null) {
return AsyncStorage // Have to return the promise
.getItem("country")
.then((value) => {
if (value == 223) {
return "https://www.website.com/usa_api"
} else {
return "https://www.website.com/api"
}
});
}
module.exports = Configure
Usage 用法
Now that we are returning the Promise, we can await it from where you want to use it 现在我们返回了Promise,我们可以在您想使用它的地方等待它
// Use the promise
Configure
.getApiUrl()
.then((apiURL) => {
// You should be getting the API URL here
})
// Or better looking code with async/await
const apiURL = await Configure.getApiUrl();
// You should be getting the API URL here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.