Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

48 строки
1.4 KiB

  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _react = require("react");
  5. var _useMounted = _interopRequireDefault(require("./useMounted"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. function useCustomEffect(effect, dependencies, isEqualOrOptions) {
  8. var isMounted = (0, _useMounted.default)();
  9. var _ref = typeof isEqualOrOptions === 'function' ? {
  10. isEqual: isEqualOrOptions
  11. } : isEqualOrOptions,
  12. isEqual = _ref.isEqual,
  13. _ref$effectHook = _ref.effectHook,
  14. effectHook = _ref$effectHook === void 0 ? _react.useEffect : _ref$effectHook;
  15. var dependenciesRef = (0, _react.useRef)();
  16. dependenciesRef.current = dependencies;
  17. var cleanupRef = (0, _react.useRef)(null);
  18. effectHook(function () {
  19. // If the ref the is `null` it's either the first effect or the last effect
  20. // ran and was cleared, meaning _this_ update should run, b/c the equality
  21. // check failed on in the cleanup of the last effect.
  22. if (cleanupRef.current === null) {
  23. var cleanup = effect();
  24. cleanupRef.current = function () {
  25. if (isMounted() && isEqual(dependenciesRef.current, dependencies)) {
  26. return;
  27. }
  28. cleanupRef.current = null;
  29. if (cleanup) cleanup();
  30. };
  31. }
  32. return cleanupRef.current;
  33. });
  34. (0, _react.useDebugValue)(effect);
  35. }
  36. var _default = useCustomEffect;
  37. exports.default = _default;