【问题标题】:How to get access to `client` object anywhere in the app with Apollo Client?如何使用 Apollo Client 在应用程序中的任何位置访问`client` 对象?
【发布时间】:2021-08-03 06:56:32
【问题描述】:

我有一个使用 Apollo Client v1 的基本应用程序:

const client = new ApolloClient({...

...

<ApolloProvider client={client}>
   <App />
</ApolloProvider>,

我想知道如何让我的应用程序的其余部分访问client 对象,以便他们可以使用client.query 进行查询?

是导出客户端然后导入另一个文件的唯一方法吗?

由于我的应用程序包含在 ApolloProvider 中,我想可能有一种方法可以使用 Apollo 特定的导入来导入 client

【问题讨论】:

标签: reactjs apollo apollo-client react-apollo


【解决方案1】:
import { useApolloClient } from '@apollo/client';

function SomeComponent() {
  const client = useApolloClient();
  // `client` is now set to the `ApolloClient` instance being used by the
  // application (that was configured using something like `ApolloProvider`)
}

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 2019-10-26
    • 2020-01-19
    • 1970-01-01
    • 2019-10-09
    • 2020-10-24
    • 2018-04-22
    • 2021-01-15
    • 2020-07-01
    相关资源
    最近更新 更多