0

I have a TouchableOpacity with a Text component inside it in my React Native app, and I want the TouchableOpacity to be only as wide as the Text. I've tried doing this:

<TouchableOpacity style={{flexDirection: 'row', flexBasis: 0, flexShrink: 1}}>
    <Text>text</Text>
</TouchableOpacity>

But the TouchableOpacity is still as wide as the whole screen. flexGrow is 0 by default, so I figured by allowing the TouchableOpacity to shrink with flexShrink and having its flexBasis be zero,

gkeenley
  • 6,088
  • 8
  • 54
  • 129

1 Answers1

3

You can use the alignself style for TouchableOpacity

<TouchableOpacity style={{backgroundColor: 'red', alignSelf: 'flex-start' }}>
    <Text>text</Text>
</TouchableOpacity>
Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50