UNPKG

954 BJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _react = require("react");
7
8/**
9 * Runs a layout effect only when the dependencies have changed, skipping the
10 * initial "on mount" run. Caution, if the dependency list never changes,
11 * the effect is **never run**
12 *
13 * ```ts
14 * const ref = useRef<HTMLInput>(null);
15 *
16 * // focuses an element only if the focus changes, and not on mount
17 * useUpdateLayoutEffect(() => {
18 * const element = ref.current?.children[focusedIdx] as HTMLElement
19 *
20 * element?.focus()
21 *
22 * }, [focusedIndex])
23 * ```
24 * @param effect An effect to run on mount
25 *
26 * @category effects
27 */
28function useUpdateLayoutEffect(fn, deps) {
29 var isFirst = (0, _react.useRef)(true);
30 (0, _react.useLayoutEffect)(function () {
31 if (isFirst.current) {
32 isFirst.current = false;
33 return;
34 }
35
36 return fn();
37 }, deps);
38}
39
40var _default = useUpdateLayoutEffect;
41exports.default = _default;
\No newline at end of file