【问题标题】:React/Redux connect(mapStateToProps)() is returning undefined for this.propsReact/Redux connect(mapStateToProps)() 为 this.props 返回 undefined
【发布时间】:2017-08-18 05:15:04
【问题描述】:

我无法让我的connect()() 组件将状态映射到道具,然后解构我的 this.props 以连接到我的 redux 存储。

我收到一个控制台错误,const {items} = this.props; 显示 items 未定义。

附件是我的代码的要点。

https://gist.github.com/ericchen0121/6bc0654563fa71ab04dd69e0bfeb6668

【问题讨论】:

  • 您是否尝试过在mapStateToProps 函数中使用控制台记录reduxState.items?也尝试在connectedItems 类的componentDidMount 中记录this.props.itemsitems 道具可能为空。
  • 你试过修改你的createStore吗?根据文档,第二个参数应该是 reducer-like 结构,但您传入了增强器?
  • 我已尝试在我的机器上运行此代码,并且商店已正确创建。 reduxState.items 不为空,我在 Redux Dev Tools 中查看。我将在晚上对此进行更多研究。

标签: reactjs redux


【解决方案1】:

我发现了错误是什么。在您的 App.js 文件中,您要使用

导入 Items 容器

import { Items } from '../containers/Items'

什么时候应该是这样的:

import Items from '../containers/Items'.

Here 是关于 ES6 中导入/导出的一个很好的答案。

【讨论】:

  • 感谢您发现错误。你说得对,当我已经在 ../containers/Items 文件中使用 export default 时,我试图解构项目 import { Items } from ...
  • 很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 2019-06-06
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
相关资源
最近更新 更多