Skip to content

Commit 3a8f8f8

Browse files
committed
refactor(useResource): fit v18 useMemo return
replace undefined with null
1 parent 9ce2188 commit 3a8f8f8

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/useResource.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function getNextState<TRequest extends Request>(
7777
data: action.type === "success" ? action.data : state.data,
7878
response,
7979
error: action.type === "error" ? action.error : undefined,
80-
isLoading: action.type === "start" ? true : false,
80+
isLoading: action.type === "start",
8181
// will be deleted
8282
other: response,
8383
};
@@ -96,27 +96,27 @@ export function useResource<T extends Request>(
9696
try {
9797
return fn(...(requestParams || [])) as Resource<Payload<T>, BodyData<T>>;
9898
} catch (error) {
99-
return undefined;
99+
return null;
100100
}
101101
// eslint-disable-next-line react-hooks/exhaustive-deps
102102
}, useDeepMemo([fn, requestParams]));
103103
const requestCache = useMemo(() => {
104104
const filter = options?.cacheFilter || RequestConfig.cacheFilter;
105+
const _cache = options?.cache ?? RequestConfig.cache;
106+
if (_cache == undefined) {
107+
return null;
108+
}
109+
105110
if (filter && typeof filter === "function") {
106111
if (fnOptions && filter(fnOptions)) {
107-
return options?.cache ?? RequestConfig.cache;
112+
return _cache;
108113
}
109-
return undefined;
110114
}
111115

112-
if (
113-
fnOptions?.method === null ||
114-
fnOptions?.method === undefined ||
115-
/^get$/i.test(fnOptions.method)
116-
) {
117-
return options?.cache ?? RequestConfig.cache;
116+
if (fnOptions?.method == null || /^get$/i.test(fnOptions.method)) {
117+
return _cache;
118118
}
119-
return undefined;
119+
return null;
120120
}, [
121121
RequestConfig.cache,
122122
RequestConfig.cacheFilter,
@@ -130,14 +130,15 @@ export function useResource<T extends Request>(
130130
fnOptions &&
131131
(getStrByFn(options?.cacheKey, fnOptions) ??
132132
getStrByFn(RequestConfig.cacheKey, fnOptions))) ||
133-
undefined
133+
null
134134
);
135135
}, [RequestConfig.cacheKey, fnOptions, options?.cacheKey, requestCache]);
136136
const cacheData = useMemo(() => {
137137
if (requestCache && cacheKey && typeof requestCache.get === "function") {
138-
return (requestCache.get(cacheKey) as Payload<T, true>) ?? undefined;
138+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
139+
return requestCache.get(cacheKey) as Payload<T, true>;
139140
}
140-
return undefined;
141+
return null;
141142
}, [cacheKey, requestCache]);
142143

143144
const [createRequest, { clear }] = useRequest(fn, {
@@ -146,7 +147,7 @@ export function useResource<T extends Request>(
146147
instance: options?.instance,
147148
});
148149
const [state, dispatch] = useReducer(getNextState, {
149-
data: cacheData,
150+
data: cacheData ?? undefined,
150151
isLoading: getDefaultStateLoading<T>(requestParams, options?.filter),
151152
...options?.defaultState,
152153
});
@@ -204,8 +205,7 @@ export function useResource<T extends Request>(
204205
const refreshRefFn = useRefFn(refresh);
205206

206207
useEffect(() => {
207-
// eslint-disable-next-line @typescript-eslint/no-empty-function
208-
let canceller: Canceler = () => {};
208+
let canceller: Canceler = () => undefined;
209209
if (requestParams) {
210210
const _c = refreshRefFn.current();
211211
if (_c) {

0 commit comments

Comments
 (0)