Skip to content

Commit

Permalink
Add onResponseChange prop for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Dec 19, 2019
1 parent d6a8eb1 commit 60f08bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface FetchProps<TData = any, TError = Error> {
| { [type: string]: (res: TData) => Promise<any> };
fetchFunction?: (url: string, options: RequestInit) => Promise<any>;
onDataChange?: (newData: TData, data: TData) => any;
onResponseChange?: (response: Response) => any;
onChange?: (result: FetchResult<TData, TError>) => void;
deps?: [any];
}
Expand Down
10 changes: 9 additions & 1 deletion src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function useFetch(props) {
promises.current.splice(0, index + 1);
}

const { onChange, onDataChange } = props;
const { onChange, onDataChange, onResponseChange } = props;

let data = undefined;
if (
Expand All @@ -159,6 +159,14 @@ function useFetch(props) {
);
}

if (
nextState.response &&
nextState.response !== state.response &&
isFunction(onResponseChange)
) {
data = onResponseChange(nextState.response);
}

if (isFunction(onChange)) {
// Always call onChange even if unmounted. Useful for `POST` requests with a redirect
onChange({
Expand Down

0 comments on commit 60f08bf

Please sign in to comment.