[英]this.state undefined in React component render function
I am newbie in ReactJS. 我是ReactJS的新手。 Following function works well:
以下功能运行良好:
_renderTable: function() {
return(
React.DOM.table(null,
React.DOM.thead( {onClick: this._sort},
React.DOM.tr(null,
this.props.headers.map(function (title, idx) {
if (this.state.sortby === idx) {
title += this.state.descending ? ' \u2191' : ' \u2193'
}
return React.DOM.th({key: idx}, title);
}, this)
)
),
React.DOM.tbody( {onDoubleClick: this._showEditor},
this._renderSearch(),
this.state.data.map(function (row, rowidx) {
return(
React.DOM.tr({key: rowidx},
row.map(function (cell, idx) {
var content = cell;
//To-Do editable field on double click
var edit = this.state.edit;
if (edit && edit.row === rowidx && edit.cell === idx) {
content = React.DOM.form({onSubmit: this._save},
React.DOM.input({
type: "text",
defaultValue: content,
})
);
}
return React.DOM.td({
key: idx,
"data-row": rowidx
}, content);
}, this)
)
);
}, this)
)
)
);
},
When I rewrite it using babel: 当我使用babel重写它时:
_renderTable: function() {
return(
<table>
<thead onClick={this._sort}>
<tr>{
this.props.headers.map(function (title, idx) {
if (this.state.sortby === idx) {
title += this.state.descending ? ' \u2191' : ' \u2193'
}
return <th key={idx}>{title}</th>
}, this)
}</tr>
</thead>
<tbody onDoubleClick={this._showEditor}>
{this._renderSearch()}
{console.log("Hello", this.state)}
{
this.state.data.map(function (row, rowidx) {
return(
<tr key={rowidx}>{
row.map(function (cell, idx) {
var content = cell;
console.log("Hello2", this);
var edit = this.state.edit;
if (edit && edit.row === rowidx && edit.cell === idx) {
content = <form onSubmit={this._save}>
<input type="text" defaultValue={content}>
</input>
</form>
}
return <td key={idx}>{content}</td>
})
}</tr>
);
})
}
</tbody>
</table>
);
},
I get following error: 我收到以下错误:
Uncaught TypeError: Cannot read property 'state' of undefined
replacing var edit = this.state.edit;
替换
var edit = this.state.edit;
with var edit = false;
使用
var edit = false;
removes the error. 消除错误。
Searching shows about binding this
in a case of calling it in functions body so I tried to do it but without luck. 搜索关于绑定显示
this
呼吁它的功能体的情况下,所以我试图做到这一点,但没有运气。 Not sure that the issue in binding because non-JSX version of code working fine. 不确定绑定中的问题,因为非JSX版本的代码可以正常工作。
It is indeed a scope issue, when iterating through map you are declaring a function, using this
inside that function will reference a different context. 实际上,这是一个范围问题,在遍历map时声明一个函数,在函数内部使用
this
函数将引用不同的上下文。 You may use an arrow function ( ) => { ... }
您可以使用箭头功能
( ) => { ... }
this.state.data.map((row, rowidx) => {
...
row.map((cell, idx) => {
...
}
})
Or assign reference to this
in another variable before your map function var self = this
或者在映射函数
var self = this
之前在另一个变量中分配this
引用
var self = this
this.state.data.map(function (row, rowidx) {
...
row.map((cell, idx) => {
var edit = self.state.edit;
...
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.