42

I'm pretty new with RN, is there anyway to wrap the content of the view, similar to Android. In Android I can adjust, Height: 'wrap-content', but somehow in RN, I can't do any wrap content.

It's either I set the height of the view, or just flex, but still not wrap the view.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mina Makhtar
  • 517
  • 1
  • 7
  • 14

5 Answers5

107

You can set the parent to wrap child component like this.

alignSelf: 'baseline'

<View style={{ alignSelf:'baseline'}}>
  <Text>Child Content</Text>
</View>

Wrap child content horizontally.

Edison D'souza
  • 4,551
  • 5
  • 28
  • 39
24

In the case of flexDirection: "row", you should use flexWrap: "wrap" to wrap the items inside.

M.javid
  • 6,387
  • 3
  • 41
  • 56
Ankit Topno
  • 375
  • 2
  • 3
7

Solution 1:

You can use alignSelf: 'baseline' tp set the parent to wrap child component like this

<View style={{ alignSelf:'baseline'}}>
  <Text>Child</Text>
</View>

Solution 2:

In the case of flexDirection: "row", you should use flexWrap: "wrap" to wrap the items inside the view.

<View style={{ flexDirection: 'row', flexWrap: 'wrap'}}>
  <Text>Child</Text>
</View>
Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80
4

If the content is vertical it should wrap by default; If the content is horizontal, things get tricky... What worked for me was putting the view container inside another view:

<View style={{alignItems: "center"}}>
    <View style={{flexDirection: "row"}}>
        <Image/><Text/>
    </View>
</View>
sorinLates
  • 245
  • 5
  • 14
-3

Views wrap their content by default if 'flex' attribute is not set.

If you need the view to fill parent width or height set 'alignSelf' attribute to "stretch".

anagaf
  • 2,120
  • 1
  • 18
  • 15
  • When flex is not set, the view does not wrap all of its children, it shrinks and children become overlapped so that the entire layout is not visible. – Brandon Sep 22 '17 at 22:27