TanStackT
TanStack2y ago
9 replies
broad-emerald

Type error in react query useMutation hook

Try to implement custom hook with useMuatation for POST request. Typescript is complaining that my createBook function Type '(bookData: Shop) => Promise' has no properties in common with type 'UseMutationOptions<Shop, Error, Shop, unknown>' . Here is an overview of my code.

useCustoumMatation.ts

import apiClient from "@/services/api/api-client";
import { useMutation } from "@tanstack/react-query";

type Shop =
{ shopCode: string;
shopName: string;
mobileNo: string;
address: string;
};

const createBook = async (bookData: Shop) => {
const { data } = await apiClient.post("/books", bookData);
return data;
};

export const useCreateBook = () => {
return useMutation<Shop, Error, Shop, unknown>(createBook,
{ onSuccess: () => {
// invalidate the query cache for 'shop' }, }); };


ShopList.ts
const {mutate} = useCustoumMatation() const onSubmit = (data:Shop) => {
mutate(data)
}

How can i properly pass the type for that error. I am currently using react query v5.28.14.
Was this page helpful?