Skip to content

Custom mutation hook with arguments #3206

Answered by TkDodo
SevenOutman asked this question in Q&A
Discussion options

You must be logged in to vote

I do it the same way. My rule of thumb is: Only variables, so things that change, should be passed to .mutate or .mutateAsync. The things that are the same for each invocation of the mutation (in the scope of my component) should go to useMutation.

This basically "binds" the instance of useMutation to a single usage. Custom hooks could even return multiple methods, like:

const { update, delete } = usePostMutations(1)

basically returning one update and one delete method for post with id 1. In the custom hook, this would be two useMutations.

Or, when reading from the router, you could scope that to within the custom hook:

function useUpdateComment() {
  const { postId } = useParams();
  re…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@SevenOutman
Comment options

@TkDodo
Comment options

@SevenOutman
Comment options

Answer selected by SevenOutman
Comment options

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

Comment options

You must be logged in to vote
0 replies
# for free to join this conversation on GitHub. Already have an account? # to comment
Category
Q&A
Labels
None yet
4 participants