You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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,因此抛出该错误。

解决方案

  1. 修正导入语句
    检查触发按钮所在组件的import代码,确保导入的是actions/courses.js中的action creator:

    // 正确导入
    import { deleteCourse } from '../actions/courses';
    

    同时删除错误的API层导入:

    // 移除这条错误导入
    import { deleteCourse } from '../api';
    
  2. 验证Redux Store配置
    你的store配置已正确应用thunk中间件,但需确认reducers的导入路径是否正确:
    当前代码中import reducers from '../src/api/reducers';路径可能有误,若你的根reducer文件位于src/reducers/index.js,应修正为:

    import reducers from './reducers';
    
  3. 确认Action Creator正确性
    你的deleteCourse action 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 01:25:25