我有以下这样的代码。
import React, {Component} from 'react'
import {AsyncStorage} from 'react-native'
export default {
baseURL: 'http://mywebsite.com/JsonApi/',
};
在代码中,我将基本URL用于api调用。现在,有时候,我想向该域添加一个子域。并且,子域值取自AsyncStorage。如何修改代码来做到这一点?
答案 0 :(得分:0)
您可以从异步存储中获取价值并将其设置为状态。
import * as React from 'react';
import { View } from 'react-native';
import { AsyncStorage } from 'react-native';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
subdomain: '',
};
}
retrieveData = async () => {
try {
const value = await AsyncStorage.getItem('your subdomain stored key');
this.setState({ subdomain: value });
const baseURL = `http://${this.state.subdomain}.mywebsite.com/JsonApi/`;
} catch (error) {
console.log(error);
}
}
componentDidMount() {
this.retrieveData();
}
render() {
return (
<View/>
);
}
}