Provider.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
  3. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
  4. exports.__esModule = true;
  5. exports["default"] = void 0;
  6. var _react = _interopRequireWildcard(require("react"));
  7. var _propTypes = _interopRequireDefault(require("prop-types"));
  8. var _Context = require("./Context");
  9. var _Subscription = require("../utils/Subscription");
  10. var _useIsomorphicLayoutEffect = require("../utils/useIsomorphicLayoutEffect");
  11. function Provider(_ref) {
  12. var store = _ref.store,
  13. context = _ref.context,
  14. children = _ref.children;
  15. var contextValue = (0, _react.useMemo)(function () {
  16. var subscription = (0, _Subscription.createSubscription)(store);
  17. subscription.onStateChange = subscription.notifyNestedSubs;
  18. return {
  19. store: store,
  20. subscription: subscription
  21. };
  22. }, [store]);
  23. var previousState = (0, _react.useMemo)(function () {
  24. return store.getState();
  25. }, [store]);
  26. (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () {
  27. var subscription = contextValue.subscription;
  28. subscription.trySubscribe();
  29. if (previousState !== store.getState()) {
  30. subscription.notifyNestedSubs();
  31. }
  32. return function () {
  33. subscription.tryUnsubscribe();
  34. subscription.onStateChange = null;
  35. };
  36. }, [contextValue, previousState]);
  37. var Context = context || _Context.ReactReduxContext;
  38. return /*#__PURE__*/_react["default"].createElement(Context.Provider, {
  39. value: contextValue
  40. }, children);
  41. }
  42. if (process.env.NODE_ENV !== 'production') {
  43. Provider.propTypes = {
  44. store: _propTypes["default"].shape({
  45. subscribe: _propTypes["default"].func.isRequired,
  46. dispatch: _propTypes["default"].func.isRequired,
  47. getState: _propTypes["default"].func.isRequired
  48. }),
  49. context: _propTypes["default"].object,
  50. children: _propTypes["default"].any
  51. };
  52. }
  53. var _default = Provider;
  54. exports["default"] = _default;