From af73f5d8e56220769900dfb9d116a61d73abe49d Mon Sep 17 00:00:00 2001 From: Guthrie Schoolar <guthrieschoolar95@gmail.com> Date: Fri, 4 Oct 2024 10:23:01 -0500 Subject: [PATCH] Ensure useFetch suspends when triggering a service call more than once useFetch experimental support for React Suspense works great when the call made is only used once. In the event of an application successfully suspending after a service call, and then triggering that same call again, the suspense mechanism does not suspend as expected. This is because the suspenseStatus ref used to keep track of the suspense status is never reset! This change makes it so that when the suspense promise is created, the suspenseStatus ref is reset so that the promise can be successfully thrown for React to catch. --- src/useFetch.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/useFetch.ts b/src/useFetch.ts index f51f921..d9b45e6 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -182,6 +182,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> { if (suspense) { return async (...args) => { + suspenseStatus.current = 'pending' suspender.current = doFetch(...args).then( (newData) => { suspenseStatus.current = 'success'