44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { UseQueryOptions, useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
|
|
|
|
export function useSafeQuery<T, K = unknown>(
|
|
queryKey: [string, K?],
|
|
queryFn: () => Promise<T>,
|
|
options?: Omit<UseQueryOptions<T>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
return useQuery({
|
|
queryKey,
|
|
queryFn,
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export function useSafeInfiniteQuery<T, K = unknown>(
|
|
queryKey: [string, K?],
|
|
queryFn: ({ pageParam }: { pageParam: number }) => Promise<T>,
|
|
getNextPageParam: (lastPage: T, allPages: T[]) => number | undefined,
|
|
// options?: Omit<UseQueryOptions<T>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
return useInfiniteQuery({
|
|
queryKey,
|
|
queryFn,
|
|
getNextPageParam,
|
|
initialPageParam: 0,
|
|
})
|
|
}
|
|
|
|
// export function useAllProductInfiniteQuery(){
|
|
// return useInfiniteQuery({
|
|
// queryKey:['product'],
|
|
// queryFn: ({ pageParam = 0 }) => getAllProducts({ page: pageParam + 1, limit: 10 }),
|
|
// getNextPageParam: (lastPage, allPages) => {
|
|
// // Esta lógica determina el 'pageParam' para la siguiente página
|
|
// const nextPage = allPages.length;
|
|
// // Puedes añadir una condición para saber si hay más páginas
|
|
// if (lastPage.data.length < 10) return undefined;
|
|
// return nextPage;
|
|
// },
|
|
// initialPageParam: 0,
|
|
// })
|
|
|
|
// }
|