router icon indicating copy to clipboard operation
router copied to clipboard

ServerFn ask for a variable even if no one is asked

Open stvncode opened this issue 1 year ago • 1 comments

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

stvncode avatar Oct 06 '24 11:10 stvncode

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.

SeanCassiere avatar Oct 08 '24 13:10 SeanCassiere