From b271cfd4e7c86e6a2cad025e0ee74fe382a93a40 Mon Sep 17 00:00:00 2001 From: Dorian Maliszewski Date: Wed, 26 Jan 2022 15:58:17 +0100 Subject: [PATCH] refactor: drop isFirstLoad to use isLoading only --- packages/use-dataloader/README.md | 13 ++++++++----- packages/use-dataloader/src/types.ts | 1 - packages/use-dataloader/src/useDataLoader.ts | 12 ++++-------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/use-dataloader/README.md b/packages/use-dataloader/README.md index 619020448..71951e81b 100644 --- a/packages/use-dataloader/README.md +++ b/packages/use-dataloader/README.md @@ -123,11 +123,16 @@ function MyComponent() { fakePromise, ) - // Will be true during the promise - if (isLoading) { + // This is the first time we load the data + if (isLoading && !data) { return
Loading...
} + // This happen when you already load the data but want to reload it + if (isLoading && data) { + return
Reloading...
+ } + // Will be true when the promise is resolved if (isSuccess) { // Will display "test" in the the div @@ -233,7 +238,7 @@ const useDataLoader = ( | Property | Description | | :----------: | :-------------------------------------------------------------------------------------------------------------------: | | isIdle | `true` if the request is not launched | -| isLoading | `true` if the request is launched | +| isLoading | `true` if the request is launched **or** enabled is `true` and isIdle is `true` | | isSuccess | `true`if the request finished successfully | | isError | `true` if the request throw an error | | isPolling | `true` if the request if `enabled` is true, `pollingInterval` is defined and the status is `isLoading` or `isSuccess` | @@ -241,5 +246,3 @@ const useDataLoader = ( | data | return the `initialData` if no data is fetched or not present in the cache otherwise return the data fetched | | error | return the error occured during the request | | reload | allow you to reload the data (it doesn't clear the actual data) | - - diff --git a/packages/use-dataloader/src/types.ts b/packages/use-dataloader/src/types.ts index fb36463d8..514b1aaa2 100644 --- a/packages/use-dataloader/src/types.ts +++ b/packages/use-dataloader/src/types.ts @@ -53,7 +53,6 @@ export interface UseDataLoaderResult { isLoading: boolean isPolling: boolean isSuccess: boolean - isFirstLoad: boolean previousData?: T reload: () => Promise } diff --git a/packages/use-dataloader/src/useDataLoader.ts b/packages/use-dataloader/src/useDataLoader.ts index bf459ab76..146b9fc5a 100644 --- a/packages/use-dataloader/src/useDataLoader.ts +++ b/packages/use-dataloader/src/useDataLoader.ts @@ -85,8 +85,10 @@ const useDataLoader = ( const cancelMethodRef = useRef<(() => void) | undefined>(request?.cancel) const isLoading = useMemo( - () => request.status === StatusEnum.LOADING, - [request.status], + () => + (enabled && request.status === StatusEnum.IDLE) || + request.status === StatusEnum.LOADING, + [request.status, enabled], ) const isIdle = useMemo( () => request.status === StatusEnum.IDLE, @@ -105,11 +107,6 @@ const useDataLoader = ( [isSuccess, isLoading, enabled, pollingInterval], ) - const isFirstLoad = useMemo( - () => (enabled && isIdle) || isLoading, - [isLoading, isIdle, enabled], - ) - useEffect(() => { if (enabled) { // launch should never throw @@ -160,7 +157,6 @@ const useDataLoader = ( data: request.getData() || (initialData as T), error: request?.error, isError, - isFirstLoad, isIdle, isLoading, isPolling,