Replies: 1 comment
-
|
The issue is that The fix: call the server function directly in import { createServerFn } from '@tanstack/start'
import { queryOptions } from '@tanstack/react-query'
// Define the server function once
const getResourceParticipations = createServerFn({ method: 'GET' })
.validator((data: { resourceId: string }) => data)
.handler(async ({ data }) => {
// fetch...
})
// Works everywhere — loader, component, wherever
export const resourceParticipationsQuery = ({ resourceId }: { resourceId: string }) =>
queryOptions({
queryKey: ['resourceParticipations', resourceId],
queryFn: () => getResourceParticipations({ data: { resourceId } }),
})Why this works
This is the isomorphic pattern TanStack Start is designed around. The server function itself is already isomorphic — you don't need When to use
|
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.
-
Hi, I'm trying to figure out how to use
queryOptionstogether withuseServerFn.I'm trying to follow the pattern of creating predefined queries and use them from react:
This works on the client, but crashes when used on the loader.
I have tried creating a helper using
createIsomorphicFnto handle both cases but it doesn't seem like it works well either.Is there a "proper" way of doing this without code duplication?
The only solution that works reliably seems to duplicate the code:
Beta Was this translation helpful? Give feedback.
All reactions