React-Native 如何从平面列表中的项目处理 onPress ???

Dan*_*iel 7 reactjs react-native react-native-ios react-native-flatlist

我的 FlatList 是无状态组件,当按下项目时,我想通过调用方法“handleOnPress”来处理 onPress。我该怎么做 ??以下是示例代码。
`

handleOnPress = () => {
  .....
}
<SampleListView
    data={prop.data}
    onPress={this.handleOnPress}
/>
const SampleListView = (props: Props) => {
  return (
    <FlatList
        style={styles.container}
        data={props.data}
        keyExtractor={item => item.id.toString()}
        renderItem={renderItem}
    />
    )
}
renderItem = ({ item }: { item: DataSample }) => {
  return (
  <TouchableWithoutFeedback onPress={ () => props.onPress}>
      ....
  </TouchableWithoutFeedback>
  )
}
Run Code Online (Sandbox Code Playgroud)

`

Tay*_*şar 3

你能试试这个吗?

handleOnPress = () => {
  .....
}
<SampleListView
    data={prop.data}
    onPress={this.handleOnPress}
/>
const SampleListView = (props: Props) => {
  return (
    <FlatList
        style={styles.container}
        data={props.data}
        keyExtractor={item => item.id.toString()}
        renderItem={renderItem}
    />
    )
}
renderItem = ({ item }: { item: DataSample }) => {
  return (
    <TouchableWithoutFeedback onPress={props.onPress}>
      <View>
        ....
      </View>
    </TouchableWithoutFeedback>
  )
}
Run Code Online (Sandbox Code Playgroud)

请注意这两个链接。

https://facebook.github.io/react-native/docs/flatlist

内部带有自定义组件的 TouchableWithoutFeedback 不会触发 onPress 回调

不同之处在于,将回调作为参数并添加视图层。