@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
30 lines (26 loc) • 878 B
JavaScript
;
var react = require('react');
var index = require('../utils/index.js');
function useMemoizedFn(fn) {
if (__DEV__) {
if (!index.isFunction(fn)) {
throw new Error("useMemoizedFn expected parameter is a function, got ".concat(typeof fn));
}
}
var fnRef = react.useRef(fn);
// why not write `fnRef.current = fn`?
// https://github.com/alibaba/hooks/issues/728
fnRef.current = react.useMemo(function () { return fn; }, [fn]);
var memoizedFn = react.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;
}
module.exports = useMemoizedFn;