【发布时间】:2018-02-19 05:50:27
【问题描述】:
我写了这个反应组件
class Row extends React.Component {
constructor(props) {
super(props);
this.style = {
display: "flex"
}
}
render(){
var rowcells = [];
for(let i = 0; i < 7; i ++) {
console.log("foo " + this.props.cells)
rowcells.push(<Cell key={i} col ={i} row={this.props.row} cell={this.props.cells[i]} handleClick={this.props.handleClick}/>)
}
return (
<div style = {this.style}>
{rowcells}
</div>
)
}
}
这个组件在我的应用程序中只被调用一次
for(let i = 6; i > 0; i--) {
rows.push(<Row key={i} row={i} cells={this.props.cells[i]} handleClick={this.props.handleClick}/>)
}
所以你可以看到 props.cells 已经定义好了。但是当我的代码运行时,它在this.props.cells[I] 行失败,说它的未定义
https://codepen.io/knows_not_much/pen/dddNRZ?editors=1111
这是此处反应教程的示例
示例和我的代码之间的唯一区别是我关闭了组件样式而不是 react 组件的函数样式。
【问题讨论】:
-
let i = 5notlet i = 6在创建 Row 组件时的 for 循环中。您正在访问一个不存在的单元格数组项。
标签: reactjs