有两个参数的函数在 React 中不起作用
我有2个文件。其中之一是用于请求的“http”文件夹,另一个是React Component。当我想要更改表格并上传个人资料照片时遇到问题。更改表单有效(userDetails),但照片对象无效(UpdatedProfilePicture)。响应返回空对象。
我有一个想法并尝试过。我更改了 URL 并删除了 userDetails,并且 formData 起作用了。但他们两个都没有工作。实际上,第一个参数起作用,但第二个参数不起作用。对于请求,我使用 React Query
组件
const { mutate } = useMutation(apiService.updateUserDetailsInfo);
const onSubmit = () => {
let formData = new FormData();
formData.append("photo", UpdatedProfilePicture);
const urlUserDetails = new URLSearchParams(userDetails);
mutate(urlUserDetails, formData);
};
http 文件夹
updateUserDetailsInfo: async (userDetails, formData) => {
const response = await httpService.patch(`/user/detailsInfo?${userDetails}`, formData);
return response?.data;
},
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;
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
USEMUNT
不支持具有多个参数的功能,但是您可以将所有数据放入一个参数中:useMutation
doesn't support functions with multiple parameters, but you can put all the data into a single parameter instead: