Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/use-dataloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Loading...</div>
}

// This happen when you already load the data but want to reload it
if (isLoading && data) {
return <div>Reloading...</div>
}

// Will be true when the promise is resolved
if (isSuccess) {
// Will display "test" in the the div
Expand Down Expand Up @@ -233,13 +238,11 @@ 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` |
| previousData | if `keepPreviousData` is true it return the last data fetched |
| 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) |


1 change: 0 additions & 1 deletion packages/use-dataloader/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export interface UseDataLoaderResult<T = unknown> {
isLoading: boolean
isPolling: boolean
isSuccess: boolean
isFirstLoad: boolean
previousData?: T
reload: () => Promise<void>
}
Expand Down
12 changes: 4 additions & 8 deletions packages/use-dataloader/src/useDataLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ const useDataLoader = <T>(
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,
Expand All @@ -105,11 +107,6 @@ const useDataLoader = <T>(
[isSuccess, isLoading, enabled, pollingInterval],
)

const isFirstLoad = useMemo(
() => (enabled && isIdle) || isLoading,
[isLoading, isIdle, enabled],
)

useEffect(() => {
if (enabled) {
// launch should never throw
Expand Down Expand Up @@ -160,7 +157,6 @@ const useDataLoader = <T>(
data: request.getData() || (initialData as T),
error: request?.error,
isError,
isFirstLoad,
isIdle,
isLoading,
isPolling,
Expand Down