Skip to content

mutationFn parameter in useMutation #1070

Answered by TkDodo
anothertempore asked this question in General
Discussion options

You must be logged in to vote

I don't see that much overhead / repeating code in your example, but if you want, most abstractions can be handled with custom hooks:

export const useMyMutation = apiPath => useMutation(payload => axios.post(apiPath, payload))

if error handling is all the same, you can do that in that hook. usage:

const [mutateUser] = useMyMutation('/api/user')
const [mutatePost] = useMyMutation('/api/post')

mutateUser({ name: 'tom' })
mutatePost({ content: 'foo' })

I don't see a way of avoiding calling the hook, which will give you a function that you can invoke eventually (later). But I've also never had the need to...

Or did you have something totally different in mind?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@anothertempore
Comment options

Answer selected by anothertempore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants