useStore.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.createStoreHook = createStoreHook;
  4. exports.useStore = void 0;
  5. var _react = require("react");
  6. var _Context = require("../components/Context");
  7. var _useReduxContext2 = require("./useReduxContext");
  8. /**
  9. * Hook factory, which creates a `useStore` hook bound to a given context.
  10. *
  11. * @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
  12. * @returns {Function} A `useStore` hook bound to the specified context.
  13. */
  14. function createStoreHook(context) {
  15. if (context === void 0) {
  16. context = _Context.ReactReduxContext;
  17. }
  18. var useReduxContext = context === _Context.ReactReduxContext ? _useReduxContext2.useReduxContext : function () {
  19. return (0, _react.useContext)(context);
  20. };
  21. return function useStore() {
  22. var _useReduxContext = useReduxContext(),
  23. store = _useReduxContext.store;
  24. return store;
  25. };
  26. }
  27. /**
  28. * A hook to access the redux store.
  29. *
  30. * @returns {any} the redux store
  31. *
  32. * @example
  33. *
  34. * import React from 'react'
  35. * import { useStore } from 'react-redux'
  36. *
  37. * export const ExampleComponent = () => {
  38. * const store = useStore()
  39. * return <div>{store.getState()}</div>
  40. * }
  41. */
  42. var useStore = /*#__PURE__*/createStoreHook();
  43. exports.useStore = useStore;