20

I have api call that looks like

const getMutationService = () => {
  return {
    createMeme: async (
      _private: string,
      templateId: string,
      file: Blob,
      memeText?: string,
      title?: string,
      tags?: Array<string>,
    ): Promise<MemeCreateMemeResponse | undefined> => {
      return await memeApi.memePost({
        _private,
        templateId,
        file,
        memeText,
        title,
        tags,
      });
    },
  };
};

and I am calling this in another hook like

const mutationService = getMutationService();



 const { mutate: createMeme } = useMutation(mutationService.createMeme);

Its throwing an error of

Argument of type '(_private: string, templateId: string, file: Blob, memeText?: string, title?: string, tags?: Array) => Promise<MemeCreateMemeResponse | undefined>' is not assignable to parameter of type 'MutationKey'.

I have done other mutation calls with the same pattern. Why is this one throwing this mutation key error? Confused

3 Answers 3

35

useMutation can only accept one parameter. If you need multiple parameters, please use an object.

Sign up to request clarification or add additional context in comments.

2 Comments

An example would be appreciated.
1

Also, the useMutation hook accepts an async function.

const foo = async (param: Param) => { ... };
// Optional: You can also specify the return type
const foo = async (param: Param): Promise<Return> => { ... };

useMutation(foo);

Comments

1

What the other answers do not mention is that mutationFn also takes only 1 param not just useMutation. example -

useMutation({
mutationKey : 'abc',
mutationFn : ({a, b ,c}) => fetch(../)
})

WRONG -> mutationFn : (a,b,c) => fetch

CORRECT -> mutationFn : ({a, b ,c}) => fetch(../)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.