@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
47 lines (44 loc) • 1.58 kB
JavaScript
import { __spreadArray, __read } from '../../_virtual/_tslib.js';
import { useRef } from 'react';
import useUpdateEffect from '../../useUpdateEffect/index.js';
var useAutoRunPlugin = function (fetchInstance, _a) {
var manual = _a.manual, _b = _a.ready, ready = _b === void 0 ? true : _b, _c = _a.defaultParams, defaultParams = _c === void 0 ? [] : _c, _d = _a.refreshDeps, refreshDeps = _d === void 0 ? [] : _d, refreshDepsAction = _a.refreshDepsAction;
var hasAutoRun = useRef(false);
hasAutoRun.current = false;
useUpdateEffect(function () {
if (!manual && ready) {
hasAutoRun.current = true;
fetchInstance.run.apply(fetchInstance, __spreadArray([], __read(defaultParams), false));
}
}, [ready]);
useUpdateEffect(function () {
if (hasAutoRun.current)
return;
if (!manual) {
hasAutoRun.current = true;
if (refreshDepsAction) {
refreshDepsAction();
}
else {
fetchInstance.refresh();
}
}
}, __spreadArray([], __read(refreshDeps), false));
return {
onBefore: function () {
if (!ready) {
return {
stopNow: true,
};
}
return {};
},
};
};
useAutoRunPlugin.onInit = function (_a) {
var _b = _a.ready, ready = _b === void 0 ? true : _b, _c = _a.manual, manual = _c === void 0 ? true : _c;
return {
loading: !manual && ready,
};
};
export { useAutoRunPlugin };