-
Notifications
You must be signed in to change notification settings - Fork 0
add fallback behaviour for is-loading #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,26 @@ | ||||||||
| import React, { ReactElement, ReactNode } from 'react' | ||||||||
| import { ReactElement, ReactNode } from 'react' | ||||||||
| import { isFunction } from '../util' | ||||||||
|
|
||||||||
| type LoadingFunction = () => ReactNode | ||||||||
| type SuccessFunction<Data> = (data: Data) => ReactNode | ||||||||
| type ErrorFunction<Error> = (error: NonNullable<Error>) => ReactNode | ||||||||
|
|
||||||||
| type Props<Data, Error> = { | ||||||||
| data?: Data | ||||||||
| error?: Error | ||||||||
| isLoading: boolean | ||||||||
| /** | ||||||||
| * The async data to show. | ||||||||
| * Note that `error` and `isLoading` take precedence over data. | ||||||||
| */ | ||||||||
| data: Data | undefined | ||||||||
| /** | ||||||||
| * Set an error to show the error state. | ||||||||
| * Note that `isLoading` takes precedence over error. | ||||||||
| */ | ||||||||
| error: Error | undefined | ||||||||
| /** | ||||||||
| * Set to a boolean value to explicitly control loading state. | ||||||||
| * If undefined, loading state is shown when there is no data (null | undefined) and no error (null | undefined). | ||||||||
| */ | ||||||||
| isLoading: boolean | undefined | ||||||||
| renderLoading?: ReactNode | LoadingFunction | ||||||||
| renderError?: ReactNode | ErrorFunction<Error> | ||||||||
| } & ( | ||||||||
|
|
@@ -36,15 +48,18 @@ const AsyncView = <Data, Error>( | |||||||
| allowMissingData = false, | ||||||||
| } = props | ||||||||
|
|
||||||||
| if (isLoading) { | ||||||||
| const isError = error !== null && error !== undefined | ||||||||
| const hasData = data !== null && data !== undefined | ||||||||
|
|
||||||||
| if (isLoading || (isLoading === undefined && !hasData && !isError)) { | ||||||||
|
||||||||
| if (isLoading || (isLoading === undefined && !hasData && !isError)) { | |
| // Show loading only if isLoading is true, or if isLoading is undefined and both data and error are undefined/null (initial state) | |
| if (isLoading === true || (isLoading === undefined && !hasData && !isError)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not true, as we check explicitly for null and undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The React import was removed but ReactElement and ReactNode types are still being imported. Since this component returns JSX (line 43, 47), React needs to be in scope for older React versions or the JSX transform configuration should be verified to ensure the automatic JSX runtime is enabled.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ok, I guess. It was automatically removed.