Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacks RPC code example #337

Open
jwhits opened this issue May 4, 2024 · 0 comments
Open

Stacks RPC code example #337

jwhits opened this issue May 4, 2024 · 0 comments

Comments

@jwhits
Copy link

jwhits commented May 4, 2024

The hono stacks page showing RPC react example with posts and gets is really useful showing how hono and RPC with tanstack query can be used for frontend and backend.

However to get the example working I had to change the way the mutation worked by adding mutationFN. Could be worth updating the page?

Current code example

const mutation = useMutation<
    InferResponseType<typeof $post>,
    Error,
    InferRequestType<typeof $post>['form']
  >(
    async (todo) => {
      const res = await $post({
        form: todo,
      })
      return await res.json()
    },
    {
      onSuccess: async () => {
        queryClient.invalidateQueries({ queryKey: ['todos'] })
      },
      onError: (error) => {
        console.log(error)
      },
    }
  )

Changed to

const mutation = useMutation<
    InferResponseType<typeof $post>,
    Error,
    InferRequestType<typeof $post>['form']
  >({
    // Mutation function
    mutationFn: async (todo) => {
      const res = await $post({
        form: todo,
      });
      return await res.json()
    },

    // Success handler
    onSuccess: async () => {
      queryClient.invalidateQueries({ queryKey: ['todos'] })
    },

    // Error handler
    onError: (error: Error) => {
      console.error(error)
    },
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant