useReduxContext.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.createReduxContextHook = createReduxContextHook;
  4. exports.useReduxContext = void 0;
  5. var _react = require("react");
  6. var _Context = require("../components/Context");
  7. /**
  8. * Hook factory, which creates a `useReduxContext` hook bound to a given context. This is a low-level
  9. * hook that you should usually not need to call directly.
  10. *
  11. * @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
  12. * @returns {Function} A `useReduxContext` hook bound to the specified context.
  13. */
  14. function createReduxContextHook(context = _Context.ReactReduxContext) {
  15. return function useReduxContext() {
  16. const contextValue = (0, _react.useContext)(context);
  17. if (process.env.NODE_ENV !== 'production' && !contextValue) {
  18. throw new Error('could not find react-redux context value; please ensure the component is wrapped in a <Provider>');
  19. }
  20. return contextValue;
  21. };
  22. }
  23. /**
  24. * A hook to access the value of the `ReactReduxContext`. This is a low-level
  25. * hook that you should usually not need to call directly.
  26. *
  27. * @returns {any} the value of the `ReactReduxContext`
  28. *
  29. * @example
  30. *
  31. * import React from 'react'
  32. * import { useReduxContext } from 'react-redux'
  33. *
  34. * export const CounterComponent = () => {
  35. * const { store } = useReduxContext()
  36. * return <div>{store.getState()}</div>
  37. * }
  38. */
  39. const useReduxContext = /*#__PURE__*/createReduxContextHook();
  40. exports.useReduxContext = useReduxContext;