0

I have 2 files. One of them is "http" folder for requests, the other one is React Component. I have a problem when I want to change the form and upload a profile photo. Changing form is working (userDetails), but the photo object not working (UpdatedProfilePicture). The response comes back empty object.

I had an idea and tried. I changed the URL and deleted userDetails, and formData worked. But both of them not worked. Actually, the first parameter works, but the second parameter does not work. For request, I use React Query

The Component

  const { mutate } = useMutation(apiService.updateUserDetailsInfo);

  const onSubmit = () => {
    let formData = new FormData();
    formData.append("photo", UpdatedProfilePicture);
    const urlUserDetails = new URLSearchParams(userDetails);

    mutate(urlUserDetails, formData);

  };

The http folder

     updateUserDetailsInfo: async (userDetails, formData) => {
      const response = await httpService.patch(`/user/detailsInfo?${userDetails}`, formData);
      return response?.data;
    },

1 Answer 1

1

useMutation doesn't support functions with multiple parameters, but you can put all the data into a single parameter instead:

updateUserDetailsInfo: async (variables) => {
  const { userDetails, formData } = variables;
  const response = await httpService.patch(`/user/detailsInfo?${userDetails}`, formData);
  return response?.data;
},

// used like:
const { mutate } = useMutation(apiService.updateUserDetailsInfo);

const onSubmit = () => {
  let formData = new FormData();
  formData.append("photo", UpdatedProfilePicture);
  const userDetails = new URLSearchParams(userDetails);

  mutate({ userDetails, formData });
};
Sign up to request clarification or add additional context in comments.

1 Comment

it's worth noticing why that is, and the answer is that .mutate takes a second options argument, where you can pass additional callbacks for onSuccess, onError and onSettled. That wouldn't be easily possible with variadic arguments to the mutateFn.

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.