Skip to content

Commit a32a0a6

Browse files
refactor: drop isFirstLoad to use isLoading only (#613)
1 parent d037dc9 commit a32a0a6

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

packages/use-dataloader/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,16 @@ function MyComponent() {
123123
fakePromise,
124124
)
125125

126-
// Will be true during the promise
127-
if (isLoading) {
126+
// This is the first time we load the data
127+
if (isLoading && !data) {
128128
return <div>Loading...</div>
129129
}
130130

131+
// This happen when you already load the data but want to reload it
132+
if (isLoading && data) {
133+
return <div>Reloading...</div>
134+
}
135+
131136
// Will be true when the promise is resolved
132137
if (isSuccess) {
133138
// Will display "test" in the the div
@@ -233,13 +238,11 @@ const useDataLoader = (
233238
| Property | Description |
234239
| :----------: | :-------------------------------------------------------------------------------------------------------------------: |
235240
| isIdle | `true` if the request is not launched |
236-
| isLoading | `true` if the request is launched |
241+
| isLoading | `true` if the request is launched **or** enabled is `true` and isIdle is `true` |
237242
| isSuccess | `true`if the request finished successfully |
238243
| isError | `true` if the request throw an error |
239244
| isPolling | `true` if the request if `enabled` is true, `pollingInterval` is defined and the status is `isLoading` or `isSuccess` |
240245
| previousData | if `keepPreviousData` is true it return the last data fetched |
241246
| data | return the `initialData` if no data is fetched or not present in the cache otherwise return the data fetched |
242247
| error | return the error occured during the request |
243248
| reload | allow you to reload the data (it doesn't clear the actual data) |
244-
245-

packages/use-dataloader/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export interface UseDataLoaderResult<T = unknown> {
5353
isLoading: boolean
5454
isPolling: boolean
5555
isSuccess: boolean
56-
isFirstLoad: boolean
5756
previousData?: T
5857
reload: () => Promise<void>
5958
}

packages/use-dataloader/src/useDataLoader.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ const useDataLoader = <T>(
8585
const cancelMethodRef = useRef<(() => void) | undefined>(request?.cancel)
8686

8787
const isLoading = useMemo(
88-
() => request.status === StatusEnum.LOADING,
89-
[request.status],
88+
() =>
89+
(enabled && request.status === StatusEnum.IDLE) ||
90+
request.status === StatusEnum.LOADING,
91+
[request.status, enabled],
9092
)
9193
const isIdle = useMemo(
9294
() => request.status === StatusEnum.IDLE,
@@ -105,11 +107,6 @@ const useDataLoader = <T>(
105107
[isSuccess, isLoading, enabled, pollingInterval],
106108
)
107109

108-
const isFirstLoad = useMemo(
109-
() => (enabled && isIdle) || isLoading,
110-
[isLoading, isIdle, enabled],
111-
)
112-
113110
useEffect(() => {
114111
if (enabled) {
115112
// launch should never throw
@@ -160,7 +157,6 @@ const useDataLoader = <T>(
160157
data: request.getData() || (initialData as T),
161158
error: request?.error,
162159
isError,
163-
isFirstLoad,
164160
isIdle,
165161
isLoading,
166162
isPolling,

0 commit comments

Comments
 (0)