reactjs - 即使已定义属性,也无法读取未定义的属性“标题”
问题描述
所以我遇到了这段代码,想把它和我创建的表结合起来。我应对并粘贴它以分解并理解它。但我一直遇到这个问题,“无法读取未定义的属性'标题'”。我仍然是新手,但我已经做了很多次这种事情,我只是踩着为什么会发生这种情况
代码:
const TableDnD = (superclass) => class extends superclass {
constructor() {
super();
this.state = this.state || {};
this.state.columnX = 0;
this.state.columnY = 0;
this.state.dragging = false;
}
dragHandle(children) {
return (
<span onMouseDown={this._onStartDrag.bind(this)}>
{children}
</span>
)
}
dragContainer(children) {
const dragColumn = this.state.dragging ? this._renderDragColumn() : null;
const styles = {
position: "relative"
};
return (
<div
onMouseUp={this._endDrag.bind(this)}
onMouseMove={this._updateStyles.bind(this)}
onMouseOver={this._isOver.bind(this)}
onMouseLeave={this._exitWithoutChange.bind(this)}
style={styles}
ref={(container) => this.container = container}
>
{children}
{dragColumn}
</div>
)
}
// Bunch of code for drag and drop table
}
class Table extends TableDnD(Component) {
constructor(props) {
super(props);
this.state = {
headers: this.props.headers, // right here the error haapens
data: this.props.data,
}
}
// other code
// render the component
render() {
const headers = this.renderHeaders();
const data = this.renderRows();
// And finally, the table must be wrapped in the dragContainer method. That's it!
return this.dragContainer(
<table className="table">
<thead>
<tr>
{headers}
</tr>
</thead>
<tbody>
{data}
</tbody>
</table>
);
}
}
export default Table
就在“标题:this.props.headers”处,与数据相同。错误:无法读取未定义的属性“标题”。
即使我声明了 header 属性。
我错过了什么吗?
解决方案
应该的props.headers
,不是this.props.headers
的。
下一行也会发生同样的事情data
推荐阅读
- python - 如何将数字插入正则表达式
- webpack - 如何从 Webpack 4 中的单个 JS 文件输出多个 CSS 文件?
- c++ - 在 do-while 循环中初始化字符串将在检查条件之前跳转到它
- css - 停止在智能手机上滚动时删除“图像缩放”效果
- javascript - 如何在Javascript中将数字数组转换为整数
- node.js - 使用 Nodejs 调用需要证书的 SOAP 服务
- php - php联系表格,是否可以检查是否收到了自动电子邮件?
- css - 带角度的 CSS 菱形 (120, 60, 120, 60)
- sonarqube - 如何在 Google Cloud Build 步骤中包含 sonarqube 扫描步骤
- python - 带有多索引数据集和重置索引的 Pandas Melt - 为什么会这样?