UNPKG

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