Return the QueryKey from useQuery
#6752
-
Generally we defining one hook that creates the query, and then reusing it all over our codebase export const useSomeThing = (someArg,Arg2):QueryResult => {
return useQuery(["getSomeThing", someArg,Arg2] , ()=> "barf")
} We have a couple of components that accept a query key so they can monitor specific queries (a refetching indicator for example) It would be nice if |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I'd recommend the
you can then pass this to Returning the values from |
Beta Was this translation helpful? Give feedback.
-
@TkDodo would you be willing to reconsider this request? I have a reusable query hook that determines the My current workaround is to use a custom import { type QueryKey, useQuery, type UseQueryResult } from '@tanstack/react-query';
export type UseQueryResultWithKey<TData = unknown, TError = unknown> = UseQueryResult<TData, TError> & {
queryKey: QueryKey;
};
export function useExampleQuery(identifiers: { id1?: string; id2?: string }): UseQueryResultWithKey<unknown, unknown> {
const queryKey = ['example', identifiers];
const query = useQuery({
queryKey: queryKey,
queryFn: () => loadFunction(identifiers)
});
return { ...query, queryKey };
} |
Beta Was this translation helpful? Give feedback.
-
I'm working through the v4 to v5 migration and have reached the deprecation of @TkDodo Your last message suggests rebuilding the Exceptions are sometimes necessary, but I wanted to call this out in case there was something I was misunderstanding! edit: While revisiting the blog I noticed your newer post on Query Options, which looks like an even better approach! |
Beta Was this translation helpful? Give feedback.
-
What about returning const query = withNotification(useQuery(options)) Since there may be multiple attempts to fetch data i don't want to spam user with same notification. So i use some notification id to filter notifications and |
Beta Was this translation helpful? Give feedback.
-
Not an answer to the original question, this is for people who find this thread for the same or a similar reason as me: TLDR: I was working on a specific case in our API where the query key contains information that is not easy to get from the returned data. I was trying to achieve batching like this example: hook Calls for [a, b, c] and for [b, c, d] get turned into two useQueries-calls with three queries each (to make use of useQueries awesome batching), and then batched to one API call of [a, b, c, d]. This would result in an API return value of I was trying to batcher-resolve this result into The ensuing research brought me here. After finding out that the key is not part of the result on purpose, I spent some time trying to come up with an alternate solution. Luckily, I thought of adding a selector to my mapped queries in useQueries: This took me some time to work out, hope it helps someone else in a similar situation in the future! |
Beta Was this translation helpful? Give feedback.
I'd recommend the
queryOptions
function to abstract those away, and export it as a re-usable object:you can then pass this to
useQuery
as well as to other functions likeinvalidateQueries
or evenuseQueries
.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.