Javascript 从asyncStorage、Apollo、React Native创建HttpLink
“阿波罗反应”:“3.1.5” “反应本机”:https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz" “世博会”:“38.0.0” 我试图从本地asyncStorage获取我的url(主要是因为我的url是在登录后从两个url中选取并分配给asyncStorage的)。 这是我已经尝试过的,但我一直在得到未定义的数据Javascript 从asyncStorage、Apollo、React Native创建HttpLink,javascript,react-native,apollo,react-apollo,Javascript,React Native,Apollo,React Apollo,“阿波罗反应”:“3.1.5” “反应本机”:https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz" “世博会”:“38.0.0” 我试图从本地asyncStorage获取我的url(主要是因为我的url是在登录后从两个url中选取并分配给asyncStorage的)。 这是我已经尝试过的,但我一直在得到未定义的数据 const asyncHttpLink = setContext(async (_, {headers, .
const asyncHttpLink = setContext(async (_, {headers, ...context}) => {
const url = await getUrlFromAsyncStorage();
return createHttpLink({
uri: url ? url : '',
...context,
});
});
然后我把它分配到我的任务中
const client = new ApolloClient({
... other property as cache, type etc. ...
link: ApolloLink.from([
... other links ...
asyncHttpLink,
]),
});
在响应时,我得到所有的属性,但数据总是未定义的
[
"variables",
"refetch",
... other 7 ...,
"error",
"called",
"data", <== always undefined
"client",
]
有人一直在追查同样的情况吗?我找到了解决办法:
const customFetch = async (_: string, options: RequestInit) => {
const url = await getUrlFromAsyncStorage();
return url ? fetch(url, options) : fetch('');
};
const asyncHttpLink = createHttpLink({
fetch: customFetch,
});
我找到了解决办法:
const customFetch = async (_: string, options: RequestInit) => {
const url = await getUrlFromAsyncStorage();
return url ? fetch(url, options) : fetch('');
};
const asyncHttpLink = createHttpLink({
fetch: customFetch,
});