@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
28 lines (25 loc) • 861 B
JavaScript
import { useRef, useMemo } from 'react';
import { isFunction } from '../utils/index.js';
function useMemoizedFn(fn) {
if (__DEV__) {
if (!isFunction(fn)) {
throw new Error("useMemoizedFn expected parameter is a function, got ".concat(typeof fn));
}
}
var fnRef = useRef(fn);
// why not write `fnRef.current = fn`?
// https://github.com/alibaba/hooks/issues/728
fnRef.current = useMemo(function () { return fn; }, [fn]);
var memoizedFn = useRef();
if (!memoizedFn.current) {
memoizedFn.current = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return fnRef.current.apply(this, args);
};
}
return memoizedFn.current;
}
export { useMemoizedFn as default };