Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touchable opacity on press not working inside SVG tags

I have a component inside an <Svg> where I have a button (<TouchableOpacity> with <View>) inside the component.

While the button onClick worked fine standalone, it's not working when I wrap the component inside the SVG.

<Svg width={'100%'} height={'100%'} viewBox='0 0 360 243' {...props}>
  <Defs>
    <LinearGradient
        id='prefix__b'
        x1={'75.7%'}
        y1={'34.3%'}
        x2={'84.6%'}
        y2={'-9.6%'}
        gradientUnits='objectBoundingBox'
    >
      <Stop offset={1} stopColor='#2ff290' />
    </LinearGradient>
  </Defs>
  <View >
    <TouchableOpacity
                      onPress={() => {
      console.log('DSDA')
      }}
      ><Text>Click me!!</Text>
    </TouchableOpacity>
  </View>
</Svg>

https://github.com/react-native-community/react-native-svg/issues/1050

like image 221
aravind_reddy Avatar asked Dec 10 '25 02:12

aravind_reddy


1 Answers

You need to wrap your svg with TouchableOpacity tag, Like this:

<TouchableOpacity
    onPress={() => {
        console.log('DSDA')
    }}
>
    <Svg width={'100%'} height={'100%'} viewBox='0 0 360 243' {...props}>
        <Defs>
            <LinearGradient
                id='prefix__b'
                x1={'75.7%'}
                y1={'34.3%'}
                x2={'84.6%'}
                y2={'-9.6%'}
                gradientUnits='objectBoundingBox'
            >
                <Stop offset={1} stopColor='#2ff290' />
            </LinearGradient>
        </Defs>
        <View ><Text>Click me!!</Text>
        </View>
    </Svg>
</TouchableOpacity>
like image 96
Majdi Mohammad Avatar answered Dec 11 '25 15:12

Majdi Mohammad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!