useDispatch.js 1.4 KB

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