我怎么知道我按下了哪个TouchableNativeFeedback?

时间:2017-08-07 15:21:05

标签: react-native

我正在尝试按FlatList中的一行并使应用程序导航到另一个页面,我正在尝试这样做而不为每个渲染创建新方法。

使用React我只会从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>
    )
}

1 个答案:

答案 0 :(得分:1)

在渲染部分中,您只需按下此类

按下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)
}

在控制台中,您将获得该行中按下的该项目的值 尝试可能这可以帮到你