UNPKG

981 BJavaScriptView Raw
1import React, { useContext } from 'react';
2var ApolloContext = React.createContext(null);
3export function ApolloProvider(_ref) {
4 var client = _ref.client,
5 children = _ref.children;
6 return React.createElement(ApolloContext.Provider, {
7 value: client
8 }, children);
9}
10export function useApolloClient(overrideClient) {
11 var client = useContext(ApolloContext); // Ensures that the number of hooks called from one render to another remains
12 // constant, despite the Apollo client read from context being swapped for
13 // one passed directly as prop.
14
15 if (overrideClient) {
16 return overrideClient;
17 }
18
19 if (!client) {
20 // https://github.com/apollographql/react-apollo/blob/5cb63b3625ce5e4a3d3e4ba132eaec2a38ef5d90/src/component-utils.tsx#L19-L22
21 throw new Error('Could not find "client" in the context or passed in as a prop. ' + 'Wrap the root component in an <ApolloProvider>, or pass an ' + 'ApolloClient instance in via props.');
22 }
23
24 return client;
25}
\No newline at end of file