【问题标题】:after migrating to apollo client 3.0, using "withApollo" hoc throwing could not find client context error迁移到 apollo 客户端 3.0 后,使用“withApollo” hoc throwing 找不到客户端上下文错误
【发布时间】:2020-04-25 20:43:15
【问题描述】:

TLDR;设法提取了一个带有问题的代码框,请有一个look here

things 在使用 react-apollo 2.5 时工作正常,现在我们已经开始迁移到 3.0。

我的 package.json 相关部分的快照

    "@apollo/client": "^3.0.0-beta.19",
    "@apollo/react-components": "^3.1.3",
    "@apollo/react-hoc": "^3.1.3",

现在我收到以下错误

Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>.

我的App最外层的组件确实是ApolloProvider,

我迁移后导入ApolloProvider的方式如下图

import { ApolloProvider } from "@apollo/client";

我还注意从迁移文档中提到的 "@apollo/react-hoc" 导入 withApollo hoc。

迁移不只是更改 package.json 和导入中的版本吗?或者在创建 apollo 客户端实例时我需要了解什么具体信息?

请在下面找到用于创建客户端实例的代码

import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';

const cache = new InMemoryCache();

const client = new ApolloClient({
  link: new HttpLink({
    uri: process.env.REACT_APP_GRAPHQLURL,
  }),
  cache,
  connectToDevTools: true
});

【问题讨论】:

    标签: react-apollo apollo-client


    【解决方案1】:

    根据迁移文档,您不应从 @apollo/react-hoc 导入 withApollo hoc

    你应该使用来自@apollo/client的 hoc 包

    import { withApollo } from "@apollo/client/react/hoc";
    

    https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/#apolloreact-hoc-and-apolloreact-components

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 1970-01-01
      • 2023-03-24
      • 2016-12-06
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2017-11-10
      相关资源
      最近更新 更多