Created
July 19, 2023 15:01
-
-
Save louis-young/78d2b4a080b4f5b9881694da77ca9778 to your computer and use it in GitHub Desktop.
Asynchronous React component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { ReactNode } from "react"; | |
interface AsynchronousComponentProps<TData> { | |
children: (data: NonNullable<TData>) => ReactNode; | |
data: TData; | |
isLoading: boolean; | |
loadingFallback: ReactNode; | |
hasError: boolean; | |
errorFallback: ReactNode; | |
} | |
export const AsynchronousComponent = <TData,>({ | |
children, | |
data, | |
isLoading, | |
loadingFallback, | |
hasError, | |
errorFallback, | |
}: AsynchronousComponentProps<TData>) => { | |
return isLoading | |
? loadingFallback | |
: hasError | |
? errorFallback | |
: data | |
? children(data) | |
: undefined; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment