From 60f08bfaeebc05efc5ba9751a4c365bd00f94a7a Mon Sep 17 00:00:00 2001 From: Sean Lynch Date: Thu, 19 Dec 2019 12:57:45 -0500 Subject: [PATCH] Add onResponseChange prop for convenience --- index.d.ts | 1 + src/Fetch.js | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 13bd6a3..cba7b61 100644 --- a/index.d.ts +++ b/index.d.ts @@ -37,6 +37,7 @@ export interface FetchProps { | { [type: string]: (res: TData) => Promise }; fetchFunction?: (url: string, options: RequestInit) => Promise; onDataChange?: (newData: TData, data: TData) => any; + onResponseChange?: (response: Response) => any; onChange?: (result: FetchResult) => void; deps?: [any]; } diff --git a/src/Fetch.js b/src/Fetch.js index a4aa0f5..6a44b52 100644 --- a/src/Fetch.js +++ b/src/Fetch.js @@ -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 ( @@ -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({