UNPKG

@rozhkov/react-useful-hooks

Version:
15 lines (14 loc) 774 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const default_values_1 = require("default-values"); // Utility hook that returns a function that never has stale dependencies, but // without changing identity, as a useCallback with dep array would. // Useful for functions that depend on external state, but // should not trigger effects when that external state changes. const useStableCallback = (callback) => { const ref = (0, react_1.useRef)(callback); ref.current = callback; return (0, react_1.useCallback)((...params) => { var _a; return (_a = ref.current) === null || _a === void 0 ? void 0 : _a.call(ref, ...params); }, default_values_1.EMPTY_ARRAY); }; exports.default = useStableCallback;