useIsomorphicLayoutEffect.js 2.2 KB

123456789101112131415161718192021222324
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.useIsomorphicLayoutEffect = exports.canUseDOM = void 0;
  4. var React = _interopRequireWildcard(require("react"));
  5. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  6. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  7. // React currently throws a warning when using useLayoutEffect on the server.
  8. // To get around it, we can conditionally useEffect on the server (no-op) and
  9. // useLayoutEffect in the browser. We need useLayoutEffect to ensure the store
  10. // subscription callback always has the selector from the latest render commit
  11. // available, otherwise a store update may happen between render and the effect,
  12. // which may cause missed updates; we also must ensure the store subscription
  13. // is created synchronously, otherwise a store update may occur before the
  14. // subscription is created and an inconsistent state may be observed
  15. // Matches logic in React's `shared/ExecutionEnvironment` file
  16. const canUseDOM = !!(typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined');
  17. exports.canUseDOM = canUseDOM;
  18. const useIsomorphicLayoutEffect = canUseDOM ? React.useLayoutEffect : React.useEffect;
  19. exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;