2017-08-07 107 views
0

我试图按FlatList中的一行,并使应用程序导航到另一个页面,我试图做到这一点,而无需为每个渲染创建新的方法。我如何知道我按下了哪个TouchableNativeFeedback?

使用做出反应,我只想从event.target获得的价值,但我无法这样做,在反应本土

这是我到目前为止有:

constructor(props){ 
    super(props) 

    this._renderListItem = ({ item }) => this.renderListItem(item) 

    this._onPressListItem =() => this.onPressListItem() 
} 

onPressListItem() { 
    // Right here I need to know which row I pressed 
    this.props.navigation.navigate('ChatScreen', {}) 
} 

renderListItem(item) { 
    return (
     <TouchableNativeFeedback onPress={this._onPressListItem}> 
      <View style={{ height: 50, backgroundColor: 'red' }}> 
       <Text>{item.name}</Text> 
      </View> 
     </TouchableNativeFeedback> 
    ) 
} 

回答

0

在渲染部分,你只需要定义项目时按TouchableNativeFeedback这样

renderListItem(item) { 
return (
    <TouchableNativeFeedback onPress={()=>{this._onPressListItem(item)}}> 
     <View style={{ height: 50, backgroundColor: 'red' }}> 
      <Text>{item.name}</Text> 
     </View> 
    </TouchableNativeFeedback> 
) 
} 

然后你就可以找出哪些特定的行为被点击

onPressListItem(items) { 
console.log(items) 
} 

在控制台你会得到 尝试可能是这可以帮助你

+0

该行中按下的票面项的值谢谢你的反应,但我用此溶液看到的问题是,每时间一个新的ListItem被创建'onPress'创建一个新的函数只是为了这个绑定,即时尝试避免这种情况。 – JCRamires

+0

你可以使用地图,因为你只需要传递items数组,然后就可以像现在一样工作。 –

相关问题