useMutation: mutationFn callback data is of type void #4912
-
I am writing the following Hook to wrap the useMutation Hook: import { useEffect, useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { Problem } from '../../../../abciBackendApi/src'
import { useDefaultApi } from '../useDefaultApi'
export function useFetchUpdate<T>(
queryKey: string,
createFetch: (requestParameters: any) => Promise<T>,
showNotifications: boolean = false
) {
const [data, setData] = useState<T>({} as T)
const queryClient = useQueryClient()
const defaultApi = useDefaultApi()
const {
isLoading,
error,
data: updateData,
mutate: update
} = useMutation<T, Problem>({
mutationFn: (formData) => createFetch.bind(defaultApi, formData)(),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [queryKey] })
},
onError: (error) => {
if (showNotifications) {
// TODO render error notification
}
}
})
// making sure data is not undefined
useEffect(() => {
if (updateData) {
setData(updateData)
}
}, [updateData])
return {
isLoading,
error,
data,
update
}
} Its purpose is to handle all update in our application that are pretty similar. Thus i can pass a function that includes the fetch request and will update on the server. Now i am wondering why the formData Type here is void, i am expecting it to be T. The mutate (L19) i am exporting as update function also only accepts void for the variables: What am i missing? I assuming passing my generic in line 20 to useMutation would be enough. Would appreciate help from any typescript wizard! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
void
.mutationFn
and let the rest be inferred:string
is just an example - fill in with the real type of formData