forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseInfiniteQuery.ts
58 lines (50 loc) · 1.65 KB
/
useInfiniteQuery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
InfiniteQueryConfig,
InfiniteQueryResult,
QueryFunction,
QueryKey,
TypedQueryFunction,
TypedQueryFunctionArgs,
} from '../core/types'
import { getQueryArgs } from '../core/utils'
import { useBaseQuery } from './useBaseQuery'
// TYPES
export interface UseInfiniteQueryObjectConfig<TResult, TError> {
queryKey: QueryKey
queryFn?: QueryFunction<TResult>
config?: InfiniteQueryConfig<TResult, TError>
}
// HOOK
// Parameter syntax with optional config
export function useInfiniteQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
queryConfig?: InfiniteQueryConfig<TResult, TError>
): InfiniteQueryResult<TResult, TError>
// Parameter syntax with query function and optional config
export function useInfiniteQuery<
TResult,
TError,
TArgs extends TypedQueryFunctionArgs
>(
queryKey: QueryKey,
queryFn: TypedQueryFunction<TResult, TArgs>,
queryConfig?: InfiniteQueryConfig<TResult, TError>
): InfiniteQueryResult<TResult, TError>
export function useInfiniteQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
queryFn: QueryFunction<TResult>,
queryConfig?: InfiniteQueryConfig<TResult, TError>
): InfiniteQueryResult<TResult, TError>
// Object syntax
export function useInfiniteQuery<TResult = unknown, TError = unknown>(
config: UseInfiniteQueryObjectConfig<TResult, TError>
): InfiniteQueryResult<TResult, TError>
// Implementation
export function useInfiniteQuery<TResult, TError>(
arg1: any,
arg2?: any,
arg3?: any
): InfiniteQueryResult<TResult, TError> {
const [queryKey, config] = getQueryArgs<TResult[], TError>(arg1, arg2, arg3)
return useBaseQuery(queryKey, { ...config, infinite: true })
}