@sirhall/use-latest-callback
Version:
React hook which returns the latest callback without changing the reference
21 lines (20 loc) • 706 B
JavaScript
import * as React from 'react';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
/**
* React hook which returns the latest callback without changing the reference.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export default function useLatestCallback(callback) {
var ref = React.useRef(callback);
var latestCallback = React.useRef(function latestCallback() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return ref.current.apply(this, args);
}).current;
useIsomorphicLayoutEffect(function () {
ref.current = callback;
});
return latestCallback;
}