reactjs 类型(typescript)上不存在mustateAsync onSuccess

0sgqnhkj  于 2022-11-29  发布在  React
关注(0)|答案(1)|浏览(113)

我有两个成功工作的突变。但是,当我尝试添加onSuccess和invalidateQueries时,我得到以下错误:

Property 'mutateAsync' does not exist on type '{ onSuccess: () => void; }

挂钩

export const useAddUserContact = () => {

    const queryClient = useQueryClient();

    return useMutation(
        (user: any) => axios.post("http://localhost:4000/userContacts", user)
            .then(response => response.data)
    ),
    {
        onSuccess: () => {
            queryClient.invalidateQueries([userContacts.fetchUserContacts])
        }
    }
};

export const useDeleteUserContact = () => {

    const queryClient = useQueryClient();

    return useMutation(
        (userId: string) => axios.delete(`http://localhost:4000/userContacts/${userId}`)
            .then(response => response.data)
    ),
    {
        onSuccess: () => {
            queryClient.invalidateQueries([userContacts.fetchUserContacts])
        }
    }
};

使用钩子

const {
        mutateAsync: addUserContactMutation
    } = useAddUserContact();

    const addContactOnClickHandler = (contact: User) => {
        addUserContactMutation(contact);
    };
sg3maiej

sg3maiej1#

我过早地关闭了useMutation调用,因此意外地使用了逗号操作符,并仅返回了该配置对象。

相关问题