Search code examples
javascriptreact-nativejsxtouchableopacity

Not working onPress in nested TouchableOpacity


Hi my custom component is wrapped in TouchableOpacity like this.


  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>
    );
  };   

In Homescreen


        <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>

When I clicked my custom component InfluencerCard, it doesn't do anything. I wonder it is because that component is in other component, so parent component block clicking on custom component. Or calling onPress function is wrong.

But I tried without parent component, it was working. What am I missing?


Solution

  • It was my mistake. The problem was not from code or components. I use Card component from @ui-kitten/components and it implements TouchableOpacity behind the scene. So I don't need to wrap with TouchableOpacity again.So just do

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