UNPKG

@automattic/wpcom-checkout

Version:
29 lines 1.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useStableCallback = useStableCallback; const react_1 = require("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. */ function useStableCallback(handler) { const handlerRef = (0, react_1.useRef)(handler); (0, react_1.useLayoutEffect)(() => { handlerRef.current = handler; }); return (0, react_1.useCallback)((...args) => { const fn = handlerRef.current; return fn(...args); }, []); } //# sourceMappingURL=use-stable-callback.js.map