reactjs - React Redux“无法读取未定义的属性'props'”
问题描述
我对 React 很陌生,我创建了一个简单的游戏。我现在决定重建它并开始使用 Redux。我用
{React.cloneElement(this.props.children, this.props)} in `Main.js`
将道具传递给 World.js 的第一个孩子:
Main.js
(...)
{React.cloneElement(this.props.children, this.props)} //props passed to World
(...)
export default Main;
然后在 World.js 中,我将道具传递给接口组件:
World.js
(...)
<Interface
{...this.props}
/>
(...)
export default World;
最后,Interface.js
问题出在哪里。在getCurrentLocation
功能上,我试图达到locations
它是组件道具之一。它可作为组件的道具使用,但在功能中显示“无法读取未定义的属性'道具'”。另一件事是我不得不从
getCurrentLocation = (event) => {...
为了getCurrentLocation (event) {...
让它工作
Interface.js
class Interface extends React.Component {
getCurrentLocation (event) { // Why is getCurrentLocation = (event) => ... not working?
event.currentTarget.classList.add('active');
const locationName = event.currentTarget.dataset.name;
const updatedLocation={
...this.props.locations[locationName], //Line with error: Cannot read property 'props' of undefined
isCurrentLocation: true,
}
(...)
}
};
render() {
return (
(...)
)
感谢所有感兴趣的人。
解决方案
像这样添加构造函数解决了这个问题
constructor(props) {
super(props);
this.getCurrentLocation = this.getCurrentLocation.bind(this);
this.setCurrentLocation = this.props.setCurrentLocation.bind(this);
}
推荐阅读
- java - Java 和分数
- java - 使用 Vert.x vertx-pg-client 从池中获取 PgConnection
- epson - ESC-POS-USB-NET 无法访问打印机异常
- go - 如何提供大量页面
- python - 在保留结构的同时划分 2 个多索引数据帧
- mern - 为什么 Axios.get 给我一个 [object Object]
- c++ - 如何检测鼠标钩子程序中的拖动
- c# - 从另一个 C# 可执行文件中在 linux 中运行 ac# 可执行文件时“没有这样的文件或目录”
- java - 创建一个独特的列表
- scipy - scipy.io.wavfile.read 和 scipy.fftpack.fft 的问题