T
TanStack2mo ago
stormy-gold

useServerFn

Hi, is useServerFn a hard requirement when using server function in react-query? docs put it like this:
const getTime = useServerFn(getServerTime)

const timeQuery = useQuery({
queryKey: 'time',
queryFn: () => getTime(),
})
const getTime = useServerFn(getServerTime)

const timeQuery = useQuery({
queryKey: 'time',
queryFn: () => getTime(),
})
how would you do it if you use the queryOptions pattern like this?
const TIME_QUERIES = {
all: ['time'],
getTime: () => queryOptions({
queryKey: [...TIME_QUERIES.all, 'getTime'],
queryFn: getServerTime,
})
}
const TIME_QUERIES = {
all: ['time'],
getTime: () => queryOptions({
queryKey: [...TIME_QUERIES.all, 'getTime'],
queryFn: getServerTime,
})
}
what's the difference?
2 Replies
foreign-sapphire
foreign-sapphire2mo ago
is useServerFn a hard requirement
no, see https://tanstack.com/start/latest/docs/framework/react/server-functions#calling-server-functions-from-hooks-and-components useServerFn simply handles redirects and notFounds thrown by the server fn. if you're not using that hook you can just catch/handle them manually
stormy-gold
stormy-goldOP2mo ago
perfect. thank you!

Did you find this page helpful?