UNPKG

1.35 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.useLayoutUpdateEffect = exports.default = void 0;
9var React = _interopRequireWildcard(require("react"));
10var _canUseDom = _interopRequireDefault(require("../Dom/canUseDom"));
11/**
12 * Wrap `React.useLayoutEffect` which will not throw warning message in test env
13 */
14var useInternalLayoutEffect = process.env.NODE_ENV !== 'test' && (0, _canUseDom.default)() ? React.useLayoutEffect : React.useEffect;
15var useLayoutEffect = function useLayoutEffect(callback, deps) {
16 var firstMountRef = React.useRef(true);
17 useInternalLayoutEffect(function () {
18 return callback(firstMountRef.current);
19 }, deps);
20
21 // We tell react that first mount has passed
22 useInternalLayoutEffect(function () {
23 firstMountRef.current = false;
24 return function () {
25 firstMountRef.current = true;
26 };
27 }, []);
28};
29var useLayoutUpdateEffect = exports.useLayoutUpdateEffect = function useLayoutUpdateEffect(callback, deps) {
30 useLayoutEffect(function (firstMount) {
31 if (!firstMount) {
32 return callback();
33 }
34 }, deps);
35};
36var _default = exports.default = useLayoutEffect;
\No newline at end of file