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

Redux Toolkit中useSelector报错:Cannot read properties of undefined (reading 'dasboard')

问题分析与解决

核心问题1:拼写错误

你在useSelector里访问的state.dasboard是拼写错误,正确写法应为state.dashboard(少写了一个字母b)。

核心问题2:Store中Reducer键名不匹配

你的代码里创建了dashboardSlice,但配置store时将其reducer挂载到了home键下:

export const store = configureStore({
  reducer: {
    home: dashboardSlice.reducer
  }
})

这意味着在useSelector中,需要通过state.home访问该slice的状态,而非state.dashboard。

修复步骤

  • 若想保持slice与store键名一致,调整store的reducer配置:
export const store = configureStore({
  reducer: {
    dashboard: dashboardSlice.reducer
  }
})

同时修正useSelector中的拼写错误:

const dashboardValue = useSelector(state => state.dashboard.value)
  • 若不想修改store键名,直接在useSelector中使用正确的键名访问:
const dashboardValue = useSelector(state => state.home.value)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:40:33