get-convex / get-convex/convex-react-query

Typescript error for useMutation

Open
#4 2 comments 5 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
41
Forks
7
PR merge metrics
No merged PRs in 30d

Description

```typescript
import { api } from "@backend/convex/_generated/api";
import { useMutation } from "@tanstack/react-query";
import { useConvexMutation } from "@convex-dev/react-query";

const mutation = useMutation({
mutationFn: useConvexMutation(api.explore.react),

onSuccess: async (data) => {

},
onError: async (error) => {

},
});

```

If I use it as specified in the document, there is no problem. However, when I try to access the API variables, TypeScript gets angry. The type of the variables is void.

```typescript
import { api } from "@backend/convex/_generated/api";
import { useMutation } from "@tanstack/react-query";
import { useConvexMutation } from "@convex-dev/react-query";

const mutation = useMutation({
mutationFn: useConvexMutation(api.explore.react),

onSuccess: async (data,variables) => {

},
onError: async (error,variables) => {

},
});

```

As a solution, if I create mutationFn as an external variable, typescript does not give an error and the type of the variables is shown correctly.

```typescript
import { api } from "@backend/convex/_generated/api";
import { useMutation } from "@tanstack/react-query";
import { useConvexMutation } from "@convex-dev/react-query";

const mutationFn = useConvexMutation(api.explore.react);

const mutation = useMutation({
mutationFn: mutationFn,

onSuccess: async (data,variables) => {

},
onError: async (error,variables) => {

},
});

```

![err1](https://github.com/user-attachments/assets/4adf4a54-ba48-4df9-9532-6fde92f9bafd)
![err2](https://github.com/user-attachments/assets/bf6c6226-43ac-43e5-a073-8375ac82c420)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.