Featured Image
Software Development

Layout with Flexbox in React Native

The Flexbox layout mechanism gives you an ability how you can construct the component in the layout. The Flexbox will solve the layout challenges. You can also use Flexbox with multiple components. Flexbox is also used to layout multiple items within the container as well.

Exploring justifyContent Property

So let’s see how we use Flexbox layout in the React Native app. Now let’s add the justifyContent property in the Flexbox. justifyContent have multiple values as listed below:

  • flex-start

flexDirection: ‘flex-start’

flexDirection: ‘flex-start’

The Default behaviours of Flexbox is shown in the below diagram. And the JSX is like
<Image />
<Text />
<Text />
It’s basically left align and vertical(column) direction.

  • flex-end

flexDirection: ‘flex-end’

flexDirection: ‘flex-end’

When add justifyContent: ‘flex-end’, the all content will shift down to bottom.

  • center

flexDirection: 'center’

flexDirection: ‘center’

When adding justifyContent: ‘center’, all content will shift to the center of the layout.

  • space-between

flexDirection: 'space-between'

flexDirection: ‘space-between’

If we add justifyContent: ‘space-between’, then the Flexbox will add equally space between all the elements. So the container takes all the elements and adds maximum equalize space between each element.

The first item is at the very top of a container and the last item is at very bottom of the container and then all the space in between the intermediate items is equalized so they are completely equal in between their last space around as much like space between.

  • space-around

flexDirection: ‘space-around’

flexDirection: ‘space-around’

Space around as much like space between but it also allocates item to the very the head of the very first item and after the very last item as well.

So, in theory, each of these white spaces in here one two three and four should be completely equal whenever we’re using space around.

In conclusion, Flexbox is a powerful layout mechanism in React Native that offers a simple and efficient way to construct components within a layout. The ‘justifyContent’ property plays an important role in positioning and aligning elements within a container and allows developers to create visually appealing and responsive user interfaces.
By mastering the different ‘justifyContent’ values, such as ‘flex-start’, ‘flex-end’, ‘center’, ‘space-between’, and ‘space-around’, you can have better control over the arrangement of components and achieve the desired layout for your React Native app.

Also read: Choosing Between React Native and Flutter: Which Framework Suits Your App Best?

author
Sanjay Kakadiya
I am an Android Developer working with Aubergine Solutions. I have expertise in Android, Android Things, Firebase, BLE, Sensors, Payment Gateways, Hardware communication, Rest API, Django Rest Framework, Amazon web services, Alexa Skills.