在嵌套的TouchableOpacity中不起作用

时间:2020-02-27 18:22:49

标签: javascript react-native jsx touchableopacity

嗨,我的自定义组件就是这样包裹在TouchableOpacity中的。


  const profileOnClick = () => {                                                                                                                                                                                                                                                        
    console.log('Card Clicked!');   
  };

  export const InfluencerCard = props => {

    const {influencer, navigation} = props;
    return (
      <TouchableOpacity onPress={() => profileOnClick()}>
        <Card>
          <Text>
            {influencer.user.name}
          </Text>
          <Text>
            {influencer.tags[0].name}
          </Text>
        </Card>
      </TouchableOpacity>
    );
  };   

在主屏幕中


        <ScrollView>
          {data.categoriesForHome.map(category => (
            <Layout key={category.id}>
              <Text>
                {category.name}({category.total})
              </Text>
              <ScrollView horizontal={true}>
                {category.influencerProfiles.map(profile => (
                  <View key={profile.id}>
                    <InfluencerCard influencer={profile} />
                  </View>
                ))}
              </ScrollView>
            </Layout>
          ))}
        </ScrollView>

当我单击自定义组件InfluencerCard时,它没有任何作用。 我想知道这是因为该组件在其他组件中,所以父组件阻止了对自定义组件的单击。或调用onPress函数是错误的。

但是我尝试了没有父组件的情况,它正在工作。 我想念什么?

1 个答案:

答案 0 :(得分:1)

这是我的错误。问题不在于代码或组件。 我使用@ ui-kitten / components中的Card组件,它在后台实现了TouchableOpacity。因此,我不需要再次使用TouchableOpacity进行包装。

<Card onPress={() => profileClick()}></Card>