-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathuseApolloClient.ts
More file actions
34 lines (30 loc) · 1.01 KB
/
useApolloClient.ts
File metadata and controls
34 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as React from "react";
import type { ApolloClient } from "@apollo/client";
import { invariant } from "@apollo/client/utilities/invariant";
import { getApolloContext } from "../context/ApolloContext.js";
/**
* @example
*
* ```jsx
* import { useApolloClient } from "@apollo/client/react";
*
* 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`)
* }
* ```
*
* @returns The `ApolloClient` instance being used by the application.
*/
export function useApolloClient(override?: ApolloClient): ApolloClient {
const context = React.useContext(getApolloContext());
const client = override || context.client;
invariant(
!!client,
'Could not find "client" in the context or passed in as an option. ' +
"Wrap the root component in an <ApolloProvider>, or pass an ApolloClient " +
"instance in via options."
);
return client;
}