UNPKG

@carbon/react

Version:

React components for the Carbon Design System

34 lines (32 loc) 1.12 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ require("../_virtual/_rolldown/runtime.js"); let react = require("react"); //#region src/internal/useSavedCallback.ts /** * Copyright IBM Corp. 2016, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ /** * Returns a stable callback reference that always points to the latest version * of a callback. This is useful when you want to avoid including the callback * in dependency arrays while still ensuring the latest callback implementation * is used. * * @param callback - The callback to track. */ const useSavedCallback = (callback) => { const savedCallback = (0, react.useRef)(callback); (0, react.useEffect)(() => { savedCallback.current = callback; }, [callback]); return (0, react.useCallback)((...args) => savedCallback.current ? savedCallback.current(...args) : void 0, []); }; //#endregion exports.useSavedCallback = useSavedCallback;