QueryOptions for cross-framework library #10735
Replies: 2 comments
This comment was marked as spam.
This comment was marked as spam.
-
|
Build your library on top of For a cross-framework library, define your query options using core types: import { queryOptions } from '@tanstack/query-core';
// Your library exports
export const userQueries = {
all: () => queryOptions({
queryKey: ['users'],
queryFn: () => fetchUsers(),
}),
detail: (id: string) => queryOptions({
queryKey: ['users', id],
queryFn: () => fetchUser(id),
}),
};Then consumers use them with their framework's hooks: // React consumer
import { useQuery } from '@tanstack/react-query';
import { userQueries } from 'your-library';
const { data } = useQuery(userQueries.detail('123'));
// Vue consumer
import { useQuery } from '@tanstack/vue-query';
import { userQueries } from 'your-library';
const { data } = useQuery(userQueries.detail('123'));The |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I see what looks like nearly duplicate code in
react-query,lit-query,vue-queryfor theirqueryOptionsmethod definitions. Not precisely the same, but darn close.If I'm developing a library using
tanstack-queryto fetch data from my service, is there a pattern to conveniently produce my own@mylib/query-coreimplementation that producesoptionsobjects that includequeryKeyandqueryFnthat makes it easy to then produce@mylib/query-reactand@mylib/query-vue, etc. Seems like that's a potential benefit of the pattern (if not the outright goal). Is there a library doing this effectively/efficiently that I should take a look at?Beta Was this translation helpful? Give feedback.
All reactions