如何从 Redux Toolkit 发送带有 RTK 查询的文件?
How to send a file with RTK Query from Redux Toolkit?
我正在尝试使用 RTK 查询突变将文件上传到 API。这是我的突变代码:
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上传文件有什么正确的方法吗?
编辑:@reduxjs/toolkit 1.6.1 已修复此问题 - 请更新您的软件包
我刚刚为此开了一个问题:https://github.com/reduxjs/redux-toolkit/issues/1239 - 感谢您提出来!
现在,您可能必须禁用该检查(您可以对州内的特定路径执行此操作,同时使用 ignoredPath 选项保留其余路径)。
我正在尝试使用 RTK 查询突变将文件上传到 API。这是我的突变代码:
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上传文件有什么正确的方法吗?
编辑:@reduxjs/toolkit 1.6.1 已修复此问题 - 请更新您的软件包
我刚刚为此开了一个问题:https://github.com/reduxjs/redux-toolkit/issues/1239 - 感谢您提出来!
现在,您可能必须禁用该检查(您可以对州内的特定路径执行此操作,同时使用 ignoredPath 选项保留其余路径)。