router
router copied to clipboard
ServerFn ask for a variable even if no one is asked
Which project does this relate to?
Router
Describe the bug
export const deleteUserFn = createServerFn('POST', async () => {
const { user } = await throwIfNoSession()
await deleteUser({ id: user.id })
return json({})
})
const { mutateAsync, isPending } = useMutation({
mutationFn: deleteUserFn,
onError: (error) => toast.error(error.message),
onSuccess: () => {
toast.success('Account deleted successfully')
setOpen(false)
navigate({ to: '/login' })
},
})
Type of the function is :
const deleteUserFn: Fetcher<undefined, JsonResponse<{}>>
So we need to do a mutateAsync(undefined) instead of a mutateAsync()
You can do this to be better :
export const deleteUserFn = createServerFn('POST', async (_params: {}) => {
const { user } = await throwIfNoSession()
await deleteUser({ id: user.id })
return json({})
})
And mutateAsync({})
But it'll be better to not have to implement anything imo
Your Example Website or App
x
Steps to Reproduce the Bug or Issue
export const deleteUserFn = createServerFn('POST', async () => {
const { user } = await throwIfNoSession()
await deleteUser({ id: user.id })
return json({})
})
const { mutateAsync, isPending } = useMutation({
mutationFn: deleteUserFn,
onError: (error) => toast.error(error.message),
onSuccess: () => {
toast.success('Account deleted successfully')
setOpen(false)
navigate({ to: '/login' })
},
})
Expected behavior
mutateAsync don't need a param
Screenshots or Videos
No response
Platform
- OS: macOS
Additional context
No response
Currently this fix is slated to go out with the server functions rewrite, since the changed API surface allows for better types.
This issue is open for anyone if they want to apply a fix to whilst server functions are in their current form.