Skip to content

Commit 60f08bf

Browse files
committed
Add onResponseChange prop for convenience
1 parent d6a8eb1 commit 60f08bf

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface FetchProps<TData = any, TError = Error> {
3737
| { [type: string]: (res: TData) => Promise<any> };
3838
fetchFunction?: (url: string, options: RequestInit) => Promise<any>;
3939
onDataChange?: (newData: TData, data: TData) => any;
40+
onResponseChange?: (response: Response) => any;
4041
onChange?: (result: FetchResult<TData, TError>) => void;
4142
deps?: [any];
4243
}

src/Fetch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function useFetch(props) {
145145
promises.current.splice(0, index + 1);
146146
}
147147

148-
const { onChange, onDataChange } = props;
148+
const { onChange, onDataChange, onResponseChange } = props;
149149

150150
let data = undefined;
151151
if (
@@ -159,6 +159,14 @@ function useFetch(props) {
159159
);
160160
}
161161

162+
if (
163+
nextState.response &&
164+
nextState.response !== state.response &&
165+
isFunction(onResponseChange)
166+
) {
167+
data = onResponseChange(nextState.response);
168+
}
169+
162170
if (isFunction(onChange)) {
163171
// Always call onChange even if unmounted. Useful for `POST` requests with a redirect
164172
onChange({

0 commit comments

Comments
 (0)