UNPKG

1.13 kBJavaScriptView Raw
1'use strict';
2
3const { useContext, useDebugValue } = require('react');
4const Cache = require('./Cache');
5const CacheContext = require('./CacheContext');
6
7/**
8 * A React hook to get the [cache context]{@link CacheContext}.
9 * @kind function
10 * @name useCache
11 * @returns {Cache} The cache.
12 * @example <caption>Ways to `import`.</caption>
13 * ```js
14 * import { useCache } from 'graphql-react';
15 * ```
16 *
17 * ```js
18 * import useCache from 'graphql-react/public/useCache.js';
19 * ```
20 * @example <caption>Ways to `require`.</caption>
21 * ```js
22 * const { useCache } = require('graphql-react');
23 * ```
24 *
25 * ```js
26 * const useCache = require('graphql-react/public/useCache');
27 * ```
28 */
29module.exports = function useCache() {
30 const cache = useContext(CacheContext);
31
32 if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
33 // eslint-disable-next-line react-hooks/rules-of-hooks
34 useDebugValue(cache);
35
36 if (cache === undefined) throw new TypeError('Cache context missing.');
37
38 if (!(cache instanceof Cache))
39 throw new TypeError('Cache context value must be a `Cache` instance.');
40
41 return cache;
42};