react-native: Nested Text with onPress / TouchableOpacity Bug
Hey there. I found an issue when rendering nested Text elements. It’s almost the exact same as this ticket: https://github.com/facebook/react-native/issues/1030
I was able to get it to sort of work. I had to add an onPress
to the Text component.
Problems:
- TypeScript says there isn’t an
onPress
on Text elements. But it does in fact work. This should probably be fixed in the type definitons. - Not possible to use TouchableOpacity, so it doesn’t feel good when pressing on these items.
When using TouchableOpacity like this:
<Text>
<Text>first part</Text>
<TouchableOpacity><Text>Second part</Text></TouchableOpacity>
</Text>
Second part doesn’t get rendered at all.
Before you suggest using a <View>
around the <Text>
instead, please look at the referenced issue. When you do that, the text runs off screen, or wraps weirdly.
TLDR; I need to add a touchable opacity inside a nested Text component. Our api returns text in blocks, the RN app needs to parse it and render an array of text elements together with different styling.
React Native version:
0.61.4
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 17
- Comments: 43
It’s 2022 and we still can’t make a proper clickable text that’s inside a paragraph.
The code above, you be able to render a paragraph, but the touchable text is not properly aligned. It will be pushed up by a few pixels and I can’t find a workaround to fix it.
We have self driving cars but can’t do a proper touchable text, ironic.
Not fixed yet!
still bugging in 2023
With a small addition to @aprilmintacpineda answer, you’ll be able to have an aligned TouchableOpacity aligned with a text
After hours of war, I found a workaround to fix the pixels that RN add to TouchableOpacity element when you wrap it within a Text. The key is to add a View element within your TouchableOpacity and wrap all the element within that View and add marginBottom to negative value to align it with text as shown in the following example:
Output :
good luck ❤️
@vinaysharma14 looks like the wrapping is broken when you do it like this 😦
Same issue here. I have the following setup:
I understand that we need to use
onPress
in the nestedText
, my example works on Android (both1
and2
can be logged). But it seems that on iOSTouchableOpacity
hijacks all touch events and doesn’t propagate events down toonPress
of theText
element. Any chance we can fix it or there are workarounds? Simply usingView
withflexDirection: row
doesn’t wrap text intelligently.@backmeupplz thanks for confirming that. Either it’s a long or single tap,
TouchableOpacity
is sufficient. So, if we are able to get it working, both taps would work. If you are seeking a solution to this, you can give a thumbs up and I can check if we can implement it in React Native.Hi @zackify @backmeupplz @Stevemoretz I guess this should work fine 😃
Hey @zackify, this should help you out 😃
just testing:
Doesn’t work! Also the prop doesn’t exist in the types.
Recently I encountered the same issue, as I had to render urls differently than a simple text in chat. I tried 3 different Approach, out of which the last approach worked pretty neatly and does what I want.
What I want:
@vinaysharma14 thank you for such a thorough investigation and for the example! It looks like this works 😃 Cheers!
Hi @backmeupplz, you may check the implementation below.
GIF Demo
Code
Package.json
@backmeupplz I visualised the code you wrote into a wireframe. Can you confirm if this is the desired behaviour?
@backmeupplz I didn’t try but it’s not OP’s requirement.