[英]Calll states, and class methods inside Object.keys().map in render
How i can call function and states inside the loop in render -我如何在渲染中调用循环内的函数和状态 -
render(){
return(
<div
{Object.keys(this.state.valueList).map(function (item) {
return(
<Input key={item} list={this.state.disableList} x={this.getPosition(item)} />
)
})}
</div>
}
getPosition(value){
.......
}
Still getting error还是报错
TypeError: Cannot read property 'getPosition' of undefined
类型错误:无法读取未定义的属性“getPosition”
same with this.state.disableList
与
this.state.disableList
相同
In your case this
is not bound and accessible, so to ensure this
is bound to your component and you can call your function, either use arrow functions , or bind this .在您的情况下,
this
未绑定且不可访问,因此为确保this
绑定到您的组件并且您可以调用您的函数,请使用箭头函数或绑定 this 。
I prefer arrow functions, like this我更喜欢箭头函数,像这样
<Input key={item} list={this.state.disableList} x={(() => this.getPosition)(item)} />
As mentioned by @Alexander T in the comments, here is how you bind this
to an iterator
( map
, forEach
, etc...)正如@Alexander T 在评论中提到的,这里是如何
bind this
到iterator
( map
、 forEach
等...)
Object.keys(this.state.valueList).map(function (item) { .... }, this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.