Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在平面列表渲染函数中调用其他函数?_Javascript_Android_Ios_Typescript_React Native - Fatal编程技术网

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}
    />
    
我喜欢第二条路