useStore.js 1.1 KB

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