@kietpt2003/react-native-core-ui
Version:
React Native Core UI components by KietPT
12 lines (11 loc) • 524 B
JavaScript
import React from "react";
// Utility hook that returns a function that never has stale dependencies, but
// without changing identity, as a useCallback with dep array would.
// Useful for functions that depend on external state, but
// should not trigger effects when that external state changes.
export function useStableCallback(cb) {
const cbRef = React.useRef(cb);
cbRef.current = cb;
const identityRetainingCb = React.useCallback((...args) => cbRef.current(...args), []);
return identityRetainingCb;
}