Created
May 2, 2024 03:41
-
-
Save tolotrasmile/ae560a8ac57001de65df95755b2ea23d to your computer and use it in GitHub Desktop.
This file contains 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 { useQuery, UseQueryOptions } from "@tanstack/react-query"; | |
import { type ReactNode } from "react"; | |
interface SuspenseQuery<TData> { | |
queryOption: UseQueryOptions<T>; | |
fallback?: ReactNode; | |
pending?: ReactNode; | |
children: (data: TData) => ReactNode; | |
} | |
export function SuspenseQuery<TData>(props: SuspenseQuery<TData>) { | |
const { isPending, error, data } = useQuery<TData>(props.queryOption); | |
if (isPending && props.pending) { | |
return props.pending; | |
} | |
if (!error && data) { | |
return props.children(data); | |
} | |
return props.fallback; | |
} |
toky-nomena
commented
May 2, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment