React Native Layout System

   Front End Development

If you’ve been working with web technologies for a while, jumping into React Native is very straightforward, especially if you are familiar with ES6. Installing the development environment is relatively easy if you are familiar with NPM. Follow the instructions on the official website to get started.

The layout system is a fundamental concept that needs to be mastered in order to create great layouts and UIs. React Native uses Flexbox to create the layouts, which is great when we need to accommodate our components and views in different screen sizes or even different devices.

Let’s start by creating a new app. In order to do this, we need to run the following command in our terminal.

$ react-native init ReactLayouts

This  command will generate a new project with a very basic application scaffolding. Open ReactLayouts.xcodeproj in Xcode and run the app in a simulator. You should see something like the below image.

React Layouts

Now we are ready to start building our app.

Understanding column and row

When we use flexbox, we can arrange the children of a container in two directions, vertically or horizontally.

By default, all children will be arranged vertically. The flex direction is called column. If we want to arrange the children horizontally, we need to change the flex direction to row.

Flex Direction

Let’s create a toolbar in the first tab. The toolbar will have one button on the right, one on the left, and a centered title. Add the following code to the index.ios.js file.

'use strict';

var React = require('react-native');
var {
} = React;
class ReactLayouts extends Component{
    render() {
        return (
                <View style={styles.toolbar}>
                    <Text style={styles.toolbarButton}>Add</Text>
                    <Text style={styles.toolbarTitle}>This is the title</Text>
                    <Text style={styles.toolbarButton}>Like</Text>
var styles = StyleSheet.create({

AppRegistry.registerComponent('ReactLayouts', () => ReactLayouts);

As you can see, inside of the render method we have a container with a style called toolbar. Inside of this container we have three different text containers. If we refresh our application, we will see that all three texts will be displayed vertically, one after another. However, we need to display the text horizontally. The title text should be flexible, depending on the device width.

In order to arrange the direction of the text horizontally, we need to change the flexDirection property of the toolbar container.

var styles = StyleSheet.create({
        flexDirection:'row'    //Step 1
        width: 50,            //Step 2
        flex:1                //Step 3
  1. Change the flex direction to the parent. In this case, the row direction will display the children horizontally.
  2. Set the width of the buttons. This step is not necessary, but if we don’t set a fixed width, then the default width of the text will be used.
  3. Set the title to flexible. This means that this container will use all the available space. This is really neat when changing from portrait to landscape because the toolbar will expand horizontally based on the available space.

Here’s the result of our new toolbar.

Flex row toolbar

Go ahead and change the simulator to landscape using Command + Right/Left arrow key (⌘+Left Arrow), you will see how the title will adjust its width in order to use all the available space.

Now let’s add some content after the toolbar. We need to add a container, and expand it to take all the available vertical space. In order to add a new container, let’s modify the render method.

render() {
    return (
        <View style={styles.mainContainer}>     //Step 1
            <View style={styles.toolbar}>
            <View style={styles.content}>            //Step 2
                <Text>This is the content</Text>
  1. Add a new style definition to the main container. We need to expand this container to take up the full screen.
  2. Define the new content. In this case, a view with text.

Now let’s style the two new elements.

var styles = StyleSheet.create({
        flex:1                  //Step 1
        flex:1                //Step 2
  1. Make the main container flexible. This way, the component will take all the available height. This is handy when changing from portrait to landscape.
  2. Define the content as flexible. We also set a background color so we can see how the container takes the available space.

Flex Row Tool Bar 2

In the left image, the content is not flexible, therefore the height depends on the content itself. In the right image, the content is flexible, the height will be adjusted to use all the available vertical space. As you can see, it’s very easy to make flexible containers in React Native.


To recap, whenever we need to arrange the children of a container horizontally, we need to set the flex direction to row. By default, the flex direction is column. This means that the children will be arranged vertically.

We can use these concepts to create responsive layouts. The width or height of a container will use all the space in the device. Our next tutorial in the series will show you how to align the children of a container.

This website uses cookies

These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience, and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.

Please consent to the use of cookies before continuing to browse our site.

Like What You See?

Got any questions?