TaskQueue: Error with task : undefined is not a function (evaluating 'this._renderReplies(replyCount)')
我在上面收到此错误。
_renderReplies = (replyCount) => {
return (<Text>`View ${replyCount} replies`</Text>);
}
_renderItem(row) {
...
<View>{this._renderReplies(replyCount)}</View> <- Getting an error here
</View>
)
}
为什么我收到未定义的函数错误????太奇怪了。
你应该将你的_renderItem
函数绑定到class
;
要么使用arrow
函数(就像你对_renderReplies
所做的那样),它会自动绑定它
_renderItem = (row) => {
...
<View>{this._renderReplies(replyCount)}</View> <- Getting an error here
</View>
)
}
或者将其绑定在constructor
中:
constructor(props){
super(props);
this._renderItem = this._renderItem.bind(this);
}
_renderItem(row) {
...
<View>{this._renderReplies(replyCount)}</View> <- Getting an error here
</View>
)
}
>_renderItem无法访问此内容。可以使用箭头函数,也可以将其绑定到构造函数中。箭头函数始终可以访问此内容。
箭头函数方法:
_renderItem = (row) => {
...
<View>{this._renderReplies(replyCount)}</View> <- Getting an error here
</View>
)
}
_renderReplies = (replyCount) => {
return (<Text>`View ${replyCount} replies`</Text>);
}
绑定方式:
constructor(props) {
this._renderItem = this._renderItem.bind(this)
this._renderReplies = this._renderReplies.bind(this)
}
_renderItem(row) {
...
<View>{this._renderReplies(replyCount)}</View> <- Getting an error here
</View>
)
}
_renderReplies(replyCount) {
return (<Text>`View ${replyCount} replies`</Text>);
}
调用 _renderReplies() 时尽量不要使用 this 关键字