UNPKG

@tanstack/react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

92 lines (76 loc) 2.68 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/_interopNamespace(React); const defaultContext = /*#__PURE__*/React__namespace.createContext(undefined); const QueryClientSharingContext = /*#__PURE__*/React__namespace.createContext(false); // If we are given a context, we will use it. // Otherwise, if contextSharing is on, we share the first and at least one // instance of the context across the window // to ensure that if React Query is used across // different bundles or microfrontends they will // all use the same **instance** of context, regardless // of module scoping. function getQueryClientContext(context, contextSharing) { if (context) { return context; } if (contextSharing && typeof window !== 'undefined') { if (!window.ReactQueryClientContext) { window.ReactQueryClientContext = defaultContext; } return window.ReactQueryClientContext; } return defaultContext; } const useQueryClient = ({ context } = {}) => { const queryClient = React__namespace.useContext(getQueryClientContext(context, React__namespace.useContext(QueryClientSharingContext))); if (!queryClient) { throw new Error('No QueryClient set, use QueryClientProvider to set one'); } return queryClient; }; const QueryClientProvider = ({ client, children, context, contextSharing = false }) => { React__namespace.useEffect(() => { client.mount(); return () => { client.unmount(); }; }, [client]); if (process.env.NODE_ENV !== 'production' && contextSharing) { client.getLogger().error("The contextSharing option has been deprecated and will be removed in the next major version"); } const Context = getQueryClientContext(context, contextSharing); return /*#__PURE__*/React__namespace.createElement(QueryClientSharingContext.Provider, { value: !context && contextSharing }, /*#__PURE__*/React__namespace.createElement(Context.Provider, { value: client }, children)); }; exports.QueryClientProvider = QueryClientProvider; exports.defaultContext = defaultContext; exports.useQueryClient = useQueryClient; //# sourceMappingURL=QueryClientProvider.js.map