Skip to content

Commit d7ec8fe

Browse files
committed
chore: add onCancel for useRequest
1 parent c7bb04c commit d7ec8fe

File tree

5 files changed

+8
-2
lines changed

5 files changed

+8
-2
lines changed

packages/hooks/src/useRequest/doc/basic/basic.en-US.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Next, we will demonstrate the difference between `run` and `runAsync` through th
6262
- `onSuccess`: Triggered when the request is resolved
6363
- `onError`: Triggered when the request is rejected
6464
- `onFinally`: Triggered when the request is completed
65+
- `onCancel`: Triggered when the request is canceled
6566

6667
<code src="./demo/lifeCycle.tsx" />
6768

packages/hooks/src/useRequest/doc/basic/basic.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const { loading, run, runAsync } = useRequest(service, {
6262
- `onSuccess`:请求成功触发
6363
- `onError`:请求失败触发
6464
- `onFinally`:请求完成触发
65+
- `onCancel`:请求取消触发
6566

6667
<code src="./demo/lifeCycle.tsx" />
6768

packages/hooks/src/useRequest/src/Fetch.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,15 @@ export default class Fetch<TData, TParams extends any[]> {
135135
});
136136
}
137137

138-
cancel() {
138+
cancel(isUnmount = false) {
139139
this.count += 1;
140140
this.setState({
141141
loading: false,
142142
});
143143

144+
if(!isUnmount){
145+
this.options.onCancel?.();
146+
}
144147
this.runPluginHandler('onCancel');
145148
}
146149

packages/hooks/src/useRequest/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface Options<TData, TParams extends any[]> {
4646
onError?: (e: Error, params: TParams) => void;
4747
// formatResult?: (res: any) => TData;
4848
onFinally?: (params: TParams, data?: TData, e?: Error) => void;
49+
onCancel?: () => void;
4950

5051
defaultParams?: TParams;
5152

packages/hooks/src/useRequest/src/useRequestImplement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function useRequestImplement<TData, TParams extends any[]>(
5656
});
5757

5858
useUnmount(() => {
59-
fetchInstance.cancel();
59+
fetchInstance.cancel(true);
6060
});
6161

6262
return {

0 commit comments

Comments
 (0)