React-Redux报错:Actions必须是纯对象,实际类型为Promise
问题解决:Uncaught Error: Actions must be plain objects. Instead, the actual type was: 'Promise'
错误原因
你在触发删除操作时,误导入并dispatch了API层的deleteCourse方法(该方法返回axios请求的Promise),而非Redux action creator(返回可被thunk中间件处理的函数)。Redux默认要求action必须是普通对象,无法直接处理Promise,因此抛出该错误。
解决方案
修正导入语句
检查触发按钮所在组件的import代码,确保导入的是actions/courses.js中的action creator:// 正确导入 import { deleteCourse } from '../actions/courses';同时删除错误的API层导入:
// 移除这条错误导入 import { deleteCourse } from '../api';验证Redux Store配置
你的store配置已正确应用thunk中间件,但需确认reducers的导入路径是否正确:
当前代码中import reducers from '../src/api/reducers';路径可能有误,若你的根reducer文件位于src/reducers/index.js,应修正为:import reducers from './reducers';确认Action Creator正确性
你的deleteCourseaction creator写法是标准的thunk格式,无需修改:export const deleteCourse = (id) => async (dispatch) => { try { await api.deleteCourse(id); dispatch({ type: DELETE, payload: id }); } catch (error) { console.log(error); } };
补充说明
Redux-thunk中间件的作用是允许action creator返回函数(而非普通对象),以此处理异步逻辑(如API请求)。只要确保dispatch的是action creator返回的函数,而非直接dispatch API请求的Promise,就能解决该问题。
内容的提问来源于stack exchange,提问作者Vlada Stefanovic
相关产品推荐
相关产品推荐

