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

Redux Toolkit使用useDispatch报reading 'type' undefined错误求助

报错根因

你遇到的报错:

Cannot read properties of undefined (reading 'type')

触发原因是index.js中存在导入错误,你传递给dispatch的参数不是合法的Redux action对象:

  • device.js中detectDevice是命名导出的action creator,文件默认导出的是slice对应的reducer函数
  • index.js中你使用默认导入语法import detectDevice from "../redux/device",实际拿到的是reducer而非action creator。调用该reducer没有返回标准带type字段的action对象,甚至直接返回undefined,Redux内部读取action的type字段时就触发了报错。
修复方案

仅需要修改index.js的导入语句,改为命名导入即可:

// 把原来的默认导入改为命名导入
import { detectDevice } from "../redux/device";

你的store.js中导入reducer用的是默认导入,符合device.js的导出规则,不需要调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 09:36:03