Skip to content

Return the QueryKey from useQuery #6752

Answered by TkDodo
shughes-uk asked this question in Ideas
Discussion options

You must be logged in to vote

I'd recommend the queryOptions function to abstract those away, and export it as a re-usable object:

export const someThingQuery = (someArg,Arg2) queryOptions({
  queryKey: ["getSomeThing", someArg,Arg2],
  queryFn: () => "barf"
})

you can then pass this to useQuery as well as to other functions like invalidateQueries or even useQueries.

Returning the values from useQuery doesn't make much sense because you can only then pass it down to components below that query, which doesn't cover cases where you need it in a sibling component. Calling the hook just to get the queryKey is also suboptimal, because it creates another subscriber. What you likely want is to abstract away the options.

Replies: 2 comments 1 reply

Comment options

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

Answer selected by shughes-uk
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
Ideas
Labels
None yet
4 participants