@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
33 lines (27 loc) • 800 B
JavaScript
/**
* MSKCC 2021, 2024
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
/**
* Provide a stable reference for a callback that is passed as a prop to a
* component. This is helpful when you want access to the latest version of a
* callback prop but don't want it to be added to the dependency array of an
* effect.
*
* @param {Function | undefined} callback
* @returns {Function}
*/
function useSavedCallback(callback) {
const savedCallback = React.useRef(callback);
React.useEffect(() => {
savedCallback.current = callback;
});
return React.useCallback(function () {
if (savedCallback.current) {
return savedCallback.current(...arguments);
}
}, []);
}
exports.useSavedCallback = useSavedCallback;