Redux-Toolkit报错:Cannot read properties of undefined (reading 'buying')
错误原因及解决方案
核心问题
你遇到的Cannot read properties of undefined (reading 'buying')错误,本质是冰淇淋操作(icecreamActions)的导入路径错误:
在Index.js中,你错误地从蛋糕切片文件(./feature/cake/cakeSlice)导入icecreamActions,但icecreamActions实际定义在冰淇淋切片文件(./feature/icecream/iceSlice.js)中。错误的导入会导致icecreamActions变量为undefined,调用其buying()方法时自然触发该报错。
修复步骤
1. 修正Index.js的导入语句
将Index.js中导入icecreamActions的代码替换为正确路径:
// 原错误代码 // const icecreamActions=require('./feature/cake/cakeSlice').icecreamActions // 修正后的代码 const icecreamActions=require('./feature/icecream/iceSlice').icecreamActions
2. 验证其他配置(已正确,可跳过)
- 你的
store.js中已正确导入冰淇淋切片的reducer(路径../feature/icecream/iceSlice),无需修改 - 你的
iceSlice.js(即提供的Iceslice.js)已正确导出icecreamActions,无需修改
修复后的完整Index.js代码
const store=require('./app/store') const cakeActions=require('./feature/cake/cakeSlice').cakeActions const icecreamActions=require('./feature/icecream/iceSlice').icecreamActions console.log("Initial State",store.getState()) const unsubscribe=store.subscribe(()=>{ console.log('Updated State',store.getState()) }) store.dispatch(cakeActions.ordered()) store.dispatch(cakeActions.restocked(5)) store.dispatch(icecreamActions.buying()) store.dispatch(icecreamActions.restocking(7)) unsubscribe()
内容的提问来源于stack exchange,提问作者Bhavishya Tomer
相关产品推荐
相关产品推荐

