React native 平面列表';s renderItem不';“不承认”;这";关键词

React native 平面列表';s renderItem不';“不承认”;这";关键词,react-native,react-native-flatlist,React Native,React Native Flatlist,所以,我最近开始在我正在开发的应用程序中重复使用FlatList。我现在正在一个屏幕上工作,屏幕上有一个请求列表,一旦一个请求被接受,它就会被更新,这是通过按下一个按钮来完成的。我正在使用一个名为getNewRequests的方法来更新请求,但它似乎不能被flatline调用,因为它只返回错误TypeError:\u this3未定义 render(){ return( <View style={GenericStyles.styles.genericContainer

所以,我最近开始在我正在开发的应用程序中重复使用FlatList。我现在正在一个屏幕上工作,屏幕上有一个请求列表,一旦一个请求被接受,它就会被更新,这是通过按下一个按钮来完成的。我正在使用一个名为getNewRequests的方法来更新请求,但它似乎不能被flatline调用,因为它只返回错误
TypeError:\u this3未定义

render(){
    return(
      <View style={GenericStyles.styles.genericContainer}>
        <Text> REQUEST SCREEN </Text>

        <FlatList
          data={this.state.requestList}
          renderItem={this.renderItem}
          keyExtractor={item => item.id}
          />

        <Button title="Voltar" color="cyan" onPress={() => this.props.navigation.goBack()}/>
      </View>
    );
  }

renderItem({item}){
    return(
      <Card
        containerStyle={{flex: 1, width: 200}}
        title={item.Username}>
          <Button color="blue" title="Accept" onPress={() => RequestService.allowRequest(item.id, (response) => {
            let rsp = JSON.parse(response);

            if(rsp.success){
              this.getNewRequests();
            }

          })}/>
      </Card>
    );
  }
我真的需要这个方法来工作,因为我需要更新屏幕的状态,并且尝试在那里键入整个方法只会返回相同的错误。在该上下文中,
始终返回
未定义的

render(){
    return(
      <View style={GenericStyles.styles.genericContainer}>
        <Text> REQUEST SCREEN </Text>

        <FlatList
          data={this.state.requestList}
          renderItem={this.renderItem}
          keyExtractor={item => item.id}
          />

        <Button title="Voltar" color="cyan" onPress={() => this.props.navigation.goBack()}/>
      </View>
    );
  }

renderItem({item}){
    return(
      <Card
        containerStyle={{flex: 1, width: 200}}
        title={item.Username}>
          <Button color="blue" title="Accept" onPress={() => RequestService.allowRequest(item.id, (response) => {
            let rsp = JSON.parse(response);

            if(rsp.success){
              this.getNewRequests();
            }

          })}/>
      </Card>
    );
  }
render(){
返回(
请求屏幕
项目id}
/>
this.props.navigation.goBack()}/>
);
}
renderItem({item}){
返回(
RequestService.allowRequest(item.id,(响应)=>{
让rsp=JSON.parse(响应);
如果(rsp.success){
这是getNewRequests();
}
})}/>
);
}

您需要在构造函数中绑定函数(或在任何需要的地方),执行以下操作:

或使用箭头功能:

renderItem = ({item}) => {
    //your function
}

执行此操作将使函数能够访问当前组件的
this

您需要在构造函数中绑定函数(或在任何需要的地方)执行以下操作:

或使用箭头功能:

renderItem = ({item}) => {
    //your function
}

执行此操作将使功能能够访问当前组件的

使用箭头功能工作正常,谢谢!请您解释一下为什么普通方法不起作用,而箭头函数起作用?使用箭头函数起作用,谢谢!请您解释一下为什么普通方法不起作用,而箭头函数起作用?