@automattic/wpcom-checkout
Version:
Functions and components used by WordPress.com checkout.
26 lines • 990 B
JavaScript
import { useCallback, useRef, useLayoutEffect } from 'react';
/*
* Return a stable callback function whose identity does not change.
*
* Even if the dependencies of the callback argument change, the returned
* callback will keep the same identity. In effect, the returned callback will
* always use the most recent version of the variables in the callback's
* closure.
*
* This is a simple implementation of the (still-in-development at the time of
* this commit) `useEvent` hook: https://beta.reactjs.org/apis/react/useEvent
*
* See https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md for
* more explanation of why this is helpful.
*/
export function useStableCallback(handler) {
const handlerRef = useRef(handler);
useLayoutEffect(() => {
handlerRef.current = handler;
});
return useCallback((...args) => {
const fn = handlerRef.current;
return fn(...args);
}, []);
}
//# sourceMappingURL=use-stable-callback.js.map