Javascript 如何在平面列表渲染函数中调用其他函数?
我有一个Javascript 如何在平面列表渲染函数中调用其他函数?,javascript,android,ios,typescript,react-native,Javascript,Android,Ios,Typescript,React Native,我有一个Flatlist,我在该渲染函数中调用了其他函数 otherFunc(){ alert('some thing') } item(data){ return( //...something.. {this.otherFunc()} <<<<<<<<<problem is here...its undefined ); } ren
Flatlist
,我在该渲染函数中调用了其他函数
otherFunc(){
alert('some thing')
}
item(data){
return(
//...something..
{this.otherFunc()} <<<<<<<<<problem is here...its undefined
);
}
render() {
<FlatList
ref={ref => this.scrollView = ref}
data={this.state.foods}
extraData={this.state}
keyExtractor={this._keyExtractor}
renderItem={this.Item}
horizontal
onEndReached={(x) => { this.loadMore() }}
onEndReachedThreshold={0.5}
/>
}
otherFunc(){
警惕(‘某物’)
}
项目(数据){
返回(
//……什么。。
{this.otherFunc()}在FlatList组件中使用this.item时,需要将此函数绑定到类,主要有3种方法:
- 在构造函数中:
contructor(props) {
this.item = this.item.bind(this);
// when using this.item everywhere, this will refer to the class in the method
}
- 如果您使用的是实验性公共类字段语法,则可以使用类字段正确绑定回调:
item = (data) => {
//now this refer to the class
}
- 或直接在组件中:
<FlatList
ref={ref => this.scrollView = ref}
data={this.state.foods}
extraData={this.state}
keyExtractor={this._keyExtractor}
renderItem={(data) => this.item(data)}
horizontal
onEndReached={(x) => { this.loadMore() }}
onEndReachedThreshold={0.5}
/>
this.scrollView=ref}
数据={this.state.foods}
extraData={this.state}
keyExtractor={this.\u keyExtractor}
renderItem={(数据)=>this.item(数据)}
水平的
onEndReached={(x)=>{this.loadMore()}
onEndReachedThreshold={0.5}
/>
我喜欢第二条路