Skip to content

Return the QueryKey from useQuery #6752

Answered by TkDodo
shughes-uk asked this question in Ideas

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: 5 comments 6 replies

You must be logged in to vote
1 reply
@csepulv

Answer selected by shughes-uk

You must be logged in to vote
1 reply
@TkDodo

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies

You must be logged in to vote
4 replies
@TkDodo

@okgoodlife

@TkDodo

@okgoodlife

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
7 participants