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

调用action报Action must be plain object的错误原因及正确实现咨询

报错原因及修复方案

错误原因

  • Redux原生仅支持接收普通对象类型的action,你当前的写法既没有正确遵循redux-thunk异步中间件的书写规范,还存在语法遗漏,触发了类型校验报错:
    1. action.js 中的getTables没有返回值,调用后返回undefined,无法被Redux识别
    2. utils.js 中的defaultAction逻辑顺序错误:redux-thunk要求action creator直接返回接收dispatch作为参数的函数,你当前的写法是先返回异步函数,异步执行完成后才返回dispatch函数,相当于dispatch时传入的是Promise对象,既不是普通action对象,也不符合thunk的函数格式要求
    3. 若未安装配置redux-thunk中间件,原生Redux本身就不支持函数类型的action,也会触发该报错

修复代码

utils.js 调整异步逻辑顺序

export const defaultAction = (constant, api) => () => {
    // 直接返回符合thunk要求的dispatch接收函数
    return async (dispatch) => {
        const result = await api();
        dispatch({type: constant, payload: result})
    };
};

action.js 补全返回值

export const getTables = () => defaultAction("GET_TEST", Api.getTables)();

确保已配置redux-thunk中间件(store配置示例)

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './你的reducer文件路径';

const store = createStore(rootReducer, applyMiddleware(thunk));
export default store;

验证

修改完成后正常dispatch(getTables())即可触发异步请求,请求完成后自动dispatch普通对象action更新store。

内容的提问来源于stack exchange,提问作者aleksf

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 18:27:01