[英]cache.writeData is not a function -> inMemoryCache error
I'm working with react and apollo client.我正在与 React 和 apollo 客户合作。 i'm get confusing error and i can't find exactly the problem.
我遇到了令人困惑的错误,我找不到确切的问题。 i have the code.
我有代码。
import logo from './logo.svg';
import './App.css';
import Pages from './pages';
import GlobalStyles from './components/GlobalStyles';
import { ApolloClient, ApolloProvider, createHttpLink, InMemoryCache } from '@apollo/client';
import { setContext } from 'apollo-link-context';
const uri = process.env.REACT_APP_BASE_URL;
const httpLink = createHttpLink({ uri });
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: localStorage.getItem('token') || ''
}
}
})
const cache = new InMemoryCache();
// configure Apollo Client
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache,
resolvers: {},
connectToDevTools: true
})
const data = {
isLoggedIn: !!localStorage.getItem('token')
}
//write the cache data on initial load
// here's the problem
cache.writeData({ data });
function App() {
return (
<ApolloProvider client={client}>
<GlobalStyles/>
<Pages/>
</ApolloProvider>
);
}
export default App;
i hope this error is cleared and removed.我希望清除并删除此错误。 so it can loaded login data.
所以它可以加载登录数据。 any help will be appreciated.
任何帮助将不胜感激。 thank you.
谢谢你。
from what I know you can't just write directly to the cache like that据我所知,你不能像那样直接写入缓存
you should be using writeQuery to write to the cache.您应该使用 writeQuery 来写入缓存。 use the folowing link for more information on how to use it.
使用以下链接获取有关如何使用它的更多信息。
https://www.apollographql.com/docs/react/caching/cache-interaction/ https://www.apollographql.com/docs/react/caching/cache-interaction/
if i'm wrong let me know then i can learn too如果我错了让我知道然后我也可以学习
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.