Image for post
Image for post
The struggle is real

Understanding Flexbox in React Native

Mastering these simple Flexbox recipes will make you a React Native UI master in no time

Getting Started

//Our views<View style={styles.container}>
<View style={styles.viewStyleOne}>
<Text style={styles.textStyle}> 1 </Text>
</View>
<View style={styles.viewStyleTwo}>
<Text style={styles.textStyle}> 2 </Text>
</View>
<View style={styles.viewStyleThree}>
<Text style={styles.textStyle}> 3 </Text>
</View>
</View>//Styleslet styles = StyleSheet.create({
container: { backgroundColor:'#4286f4'},
viewStyleOne: {
width:40,
height:40,
justifyContent: 'center',
alignItems:'center',
backgroundColor:'#b642f4'
},
textStyle:{
textAlign:'center'
}
})

Which renders as so:

Image for post
Image for post
Initial View

Styling the Container

container: { 
backgroundColor:'#4286f4',
flex: 1
}

This makes container fill its parent, i.e. whole screen.

Image for post
Image for post
flex : 1

Now we add:

container: { 
backgroundColor:'#4286f4',
flex: 1,
flexDirection:'row'
}

Each view’s flexDirection is set to colum by default but setting it to ‘row’ will change the orientation of the items in the container.

Image for post
Image for post
flexDirection set to ‘row’

Now we can control the orientation of the content using flexDirection.
Now lets add justifyContent and alignItems :

container:{ 
backgroundColor:'#4286f4',
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems:'flex-start'
}
Image for post
Image for post
justifyContent: ‘flex-end’

Similarly for:

container: { 
backgroundColor:'#4286f4',
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems:'flex-end'
}

Views will render like:

Image for post
Image for post
alignItems:’flex-end’
  • flexDirection determines the primary axis as ‘row’ or ‘column’.
  • justifyContent determines distribution of children along primary axis.
  • alignItems determines the alignment of children along the secondary axis.

To set items to center :

justifyContent: 'center',
alignItems:'center'
Image for post
Image for post
Items to center

justifyContent supports flex-start, center, flex-end, space-around, and space-between.

For space-around:

Image for post
Image for post
space-around

and space-between:

Image for post
Image for post
space-between

alignItems supports: flex-start, center, flex-end, and stretch.

Overriding the Container Style

alignSelf overrides alignItems and supports these options auto, flex-start, flex-end, center, stretch and baseline.

If we tell an item to align itself to flex-start,

alignSelf: 'flex-start' 

It would end up like this:

Image for post
Image for post
telling the item to align at flex start

flexGrow controls how much the item will grow relative to the rest of the flexible items inside the same container.

flexGrow: 1

Would render as:

Image for post
Image for post
flexGrow

flexBasis controls the item size with percent. For eg:

flexBasis:50 //firstView style
flexBasis:20 //secondView style
flexBasis:30 //ThirdView style
Image for post
Image for post
flexBasis

Thanks for Reading!

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and…

Navdeep Singh Bedi

Written by

Software developer and learner. https://navdroid.github.io/

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and Mobile Design resources from around the web. To submit stories for review or for inquiries about sponsorship: [email protected].

Navdeep Singh Bedi

Written by

Software developer and learner. https://navdroid.github.io/

React Native Coach

Bringing you the best React Native, GraphQL, Redux, and Mobile Design resources from around the web. To submit stories for review or for inquiries about sponsorship: [email protected].

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store