UNPKG

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