admin管理员组文章数量:1220789
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>
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
Share Improve this question edited Mar 8, 2020 at 15:39 Henfs 5,41112 gold badges31 silver badges47 bronze badges asked Jul 13, 2019 at 12:54 aravind_reddyaravind_reddy 5,4664 gold badges27 silver badges41 bronze badges 1 |7 Answers
Reset to default 1You 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>
The reason for that is that SVG is at the top as parent element, disabling the accessibility to TouchableOpacity
. This means the press command is unreachable for TouchableOpacity
. In short use single parent element.
In any case where touchable opacity does not register press event, it may depends on where you import touchable opacity from. try importing it from 'react-native' itself, not some community modules
wrapping your svg inside Touchable should able to make recognise touch for svg.Try wrapping it inside touchable component.
Use TouchableOpacity as a wrapper for the SVG and everything that you need inside.
Get touchable out SVG not a supported component atm
<Svg width={'100%'} height={'100%'} viewBox='0 0 360 243'
{...props}>
<View>
<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 >
<View>
<TouchableOpacity
onPress={() => {
console.log('DSDA')
}}
><Text>Click me!!</Text>
</TouchableOpacity>
</View>
</Svg>
You can try this just wrap the Defs by View this could work.
本文标签: javascriptTouchable opacity on press not working inside SVG tagsStack Overflow
版权声明:本文标题:javascript - Touchable opacity on press not working inside SVG tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739280020a2156195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
onPress
to aText
orCircle
element, probably other elements in the library. I just searched "Touchable" in the Issues on the repo and there are several examples of this. That fixed my problem. – Sara Inés Calderón Commented Jul 8, 2024 at 17:43