@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
29 lines (25 loc) • 713 B
JavaScript
/**
* MSKCC 2021, 2024
*/
import { useRef, useEffect, useCallback } from '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 = useRef(callback);
useEffect(() => {
savedCallback.current = callback;
});
return useCallback(function () {
if (savedCallback.current) {
return savedCallback.current(...arguments);
}
}, []);
}
export { useSavedCallback };