Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

feat(useFetchData): 增加错误抛出 #636

Merged
merged 1 commit into from
Jul 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/useFetchData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ const useFetchData = <T extends RequestData<any>>(
},
): UseFetchDataAction<T> => {
let isMount = true;
const {
defaultPageSize = 20,
defaultCurrent = 1,
onLoad = () => null,
onRequestError = () => null,
} = options || {};
const { defaultPageSize = 20, defaultCurrent = 1, onLoad = () => null, onRequestError } =
options || {};

const [list, setList] = useState<T['data']>(defaultData as any);
const [loading, setLoading] = useState<boolean | undefined>(undefined);
Expand Down Expand Up @@ -95,7 +91,12 @@ const useFetchData = <T extends RequestData<any>>(
onLoad(data);
}
} catch (e) {
onRequestError(e);
// 如果没有传递这个方法的话,需要把错误抛出去,以免吞掉错误
if (onRequestError === undefined) {
throw new Error(e);
} else {
onRequestError(e);
}
} finally {
setLoading(false);
}
Expand Down