UNPKG

flipper-plugin

Version:

Flipper Desktop plugin SDK and components

31 lines 1.19 kB
"use strict"; /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useMakeStableCallback = void 0; const react_1 = require("react"); const useLatestRef_1 = require("./useLatestRef"); /** * This hook can be used to avoid forcing consumers of a component to wrap their callbacks * in useCallback, by creating wrapper callback that redirects to the lastest prop passed in. * * Use this hook if you would like to avoid that passing a new callback to this component, * will cause child components to rerender when the callback is passed further down. * * Use it like: `const onSelect = useMakeStableCallback(props.onSelect)`. * @param fn */ function useMakeStableCallback(fn) { const latestFn = (0, useLatestRef_1.useLatestRef)(fn); return (0, react_1.useCallback)((...args) => { return latestFn.current?.apply(null, args); }, [latestFn]); } exports.useMakeStableCallback = useMakeStableCallback; //# sourceMappingURL=useMakeStableCallback.js.map