T
TanStack3w ago
exotic-emerald

passing arguments to collection?

Is there a pattern for passing a customizable query param that can be passed on to the queryFn? I'm thinking of just having a useMemo'd -> context collection for this, but wondering if there's a different approach already
3 Replies
exotic-emerald
exotic-emeraldOP3w ago
what I mean is:
createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['users/posts/', user.id],
queryFn: async () => {
// userId here!
return await fetch(`/api/users/posts/${user.id}`).then(res => res.json());
},
getKey: item => item.id,
enabled: !!user.id,
}),
),
createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['users/posts/', user.id],
queryFn: async () => {
// userId here!
return await fetch(`/api/users/posts/${user.id}`).then(res => res.json());
},
getKey: item => item.id,
enabled: !!user.id,
}),
),
obviously that id may change in the future
rare-sapphire
rare-sapphire3w ago
GitHub
Ability to create collections with parameterized query functions ·...
After reading Tanstack DB documentation, I was not able to find an example that fetches data from server with parameters. All examples show how to fetch all the data. I need to use project ID to fe...
exotic-emerald
exotic-emeraldOP3w ago
ah I see, thanks Kyle, it's more or less what I had in mind.

Did you find this page helpful?