本文介绍了为孩子反应原生 FlatList onPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为嵌套在 React Native FlatList 中的图像连接一个新闻处理程序.我已经验证了该函数是通过 props 传入的,方法是直接在我的组件内部调用该函数,并且工作正常.下面是一个简化的测试用例.我也试过在图像上设置 onPress,结果相同.

I'm trying to wire up a press handler for an image that's nested within a React Native FlatList. I've verified that the function is being passed in via props, by calling the function directly inside my component and that worked fine. Below is a reduced test case. I've also tried setting the onPress on the Image, with identical results.

const PostList = ({posts, onActLike, currentUser}) => {
  return (
    <FlatList
      data={ posts }
      keyExtractor={ (item) => item.id }
      renderItem={ ({item}) => {
        return (
          <View>
            <Image
              source={ {uri: item.media.url} }
              resizeMode="cover"
            />
            <View>
              <View
                onPress={ (item) => {
                  onActLike(item);
                } }
              >
                {
                  currentUser.likedMedia.indexOf(item.id) > -1 &&
                    <Image
                      source={ require('../assets/images/like_filled.png') }
                      style={ {width: 20, height: 17} }
                      resizeMode='contain'
                    />
                }
                {
                  currentUser.likedMedia.indexOf(item.id) === -1 &&
                    <Image
                      source={ require('../assets/images/like_unfilled.png') }
                      style={ {width: 20, height: 17} }
                      resizeMode='contain'
                    />
                }
              </View>
            </View>
          </View>
        )
      } }
    />
  )
}

推荐答案

View 不接受 onPress 函数,Image 也不接受.您需要将视图包装在可触摸(TouchableOpacity、TouchableHighlight 等)中

View doesn't accept an onPress function nor does Image. You need to wrap the View in a Touchable (TouchableOpacity, TouchableHighlight, etc)

这篇关于为孩子反应原生 FlatList onPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:39
查看更多