UNPKG

graphql-react

Version:

A GraphQL client for React using modern context and hooks APIs that’s lightweight (< 4 kB) but powerful; the first Relay and Apollo alternative with server side rendering.

24 lines (16 loc) 533 B
// @ts-check import React from "react"; import Cache from "./Cache.mjs"; import CacheContext from "./CacheContext.mjs"; /** * React hook to use the {@linkcode CacheContext}. * @returns {Cache} The cache. */ export default function useCache() { const cache = React.useContext(CacheContext); React.useDebugValue(cache); if (cache === undefined) throw new TypeError("Cache context missing."); if (!(cache instanceof Cache)) throw new TypeError("Cache context value must be a `Cache` instance."); return cache; }