Skip to content

How to create a custom useQuery hook with optional configuration options (useQueryOption)❓ #3227

Answered by TkDodo
cliffordfajardo asked this question in Q&A
Discussion options

You must be logged in to vote

This is not a type inference issue. UseQueryOptions has 4 generics. If you just write UseQueryOptions, all default values for the generics are chosen:

export interface UseQueryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> extends UseBaseQueryOptions<

if that doesn't match your actual types, you get mismatches.

Passing one generic as @mfpopa suggests will solve most of the issues, but select won't work and the queryKey will not have the correct type in the queryFunctionContext.

My usual advice would be to not create abstractions …

Replies: 7 comments 13 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
12 replies
@TkDodo
Comment options

@thatCarlyleGuy
Comment options

@LeoCaprile
Comment options

@ArturKustyaev
Comment options

@WesleyYue
Comment options

Answer selected by cliffordfajardo
Comment options

You must be logged in to vote
1 reply
@TkDodo
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet