我将尝试使用RTK Query mutations来上传文件到API。这是我的mutation代码:
addBanner: builder.mutation({
query(body) {
return {
url: `/api/banners`,
method: 'POST',
body,
}
},
})
以下是我生成请求数据的方法。
const [addBanner, { isBannerLoading }] = useAddBannerMutation();
const new_banner = new FormData();
new_banner.append("file", my_file);
new_banner.append("type", my_type);
new_banner.append("title", my_title);
addBanner(new_banner).unwrap().then( () => ...
但是我遇到了一个错误:
A non-serializable value was detected in the state, in the path: `api.mutations.L-Mje7bYDfyNCC4NcxFD3.originalArgs.file`...
我知道我可以通过中间件完全禁用非序列化检查,但我认为这不是使用Redux Toolkit和RTK的正确方式。没有文件时一切正常。有没有使用RTK上传文件的正确方法?