useStore.js 1.1 KB

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