logo
React Native Practical - Interview Questions and Answers
What are Flexbox properties in React Native?
Flexbox in React Native

React Native uses Flexbox for layout, just like CSS, but with some differences. Flexbox helps create responsive UIs that work on different screen sizes.


1. Main Flexbox Properties

Here are the key Flexbox properties in React Native:

Property Description
flex Defines how much space an item should take.
flexDirection Sets the direction of children (row or column).
justifyContent Aligns children along the main axis.
alignItems Aligns children along the cross axis.
alignSelf Aligns a single item on the cross axis.
flexWrap Wraps items if they exceed the container size.
gap, rowGap, columnGap Adds spacing between elements (React Native 0.71+).

2. How flex Works

The flex property determines how much space an item takes.

<View style={{ flex: 1, backgroundColor: 'lightblue' }} />
<View style={{ flex: 2, backgroundColor: 'lightgreen' }} />

* The second view takes twice the space of the first.


3. flexDirection: Row vs. Column

By default, React Native uses column, unlike CSS, which defaults to row.

<View style={{ flexDirection: 'row' }}>
  <View style={{ width: 50, height: 50, backgroundColor: 'red' }} />
  <View style={{ width: 50, height: 50, backgroundColor: 'blue' }} />
</View>

* Options:

  • row → Horizontal layout
  • column (default) → Vertical layout
  • row-reverse → Reverse horizontal
  • column-reverse → Reverse vertical

4. justifyContent: Align Items on the Main Axis

Used to position items along the main axis (horizontal for row, vertical for column).

<View style={{ flex: 1, justifyContent: 'center' }}>
  <Text>Centered</Text>
</View>

* Values :

  • flex-start (default) → Items start at the beginning
  • center → Items centered
  • flex-end → Items at the end
  • space-between → Space between items
  • space-around → Space around items
  • space-evenly → Equal spacing

5. alignItems: Align Items on the Cross Axis

Controls alignment along the cross axis (opposite of justifyContent).

<View style={{ flex: 1, alignItems: 'center' }}>
  <Text>Aligned Center</Text>
</View>

* Values :

  • flex-start → Items at start
  • center → Items in the middle
  • flex-end → Items at end
  • stretch (default) → Items stretch to fill the container

6. alignSelf: Align a Single Item

Overrides alignItems for a specific child.

<Text style={{ alignSelf: 'flex-end' }}>I move alone</Text>

7. flexWrap: Wrapping Items

By default, items stay in a single row/column. Use flexWrap to wrap them.

<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
  <View style={{ width: 100, height: 50, backgroundColor: 'red' }} />
  <View style={{ width: 100, height: 50, backgroundColor: 'blue' }} />
  <View style={{ width: 100, height: 50, backgroundColor: 'green' }} />
</View>

* Values :

  • nowrap (default) → No wrapping
  • wrap → Items wrap to the next line

8. gap, rowGap, columnGap (React Native 0.71+)

These properties add spacing between items.

<View style={{ flexDirection: 'row', gap: 10 }}>
  <View style={{ width: 50, height: 50, backgroundColor: 'red' }} />
  <View style={{ width: 50, height: 50, backgroundColor: 'blue' }} />
</View>

*  rowGap: Controls gap between rows.
*  columnGap: Controls gap between columns.


9. Example: Responsive Layout Using Flexbox
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

const App = () => {
  return (
    <View style={styles.container}>
      <View style={styles.box}><Text>1</Text></View>
      <View style={styles.box}><Text>2</Text></View>
      <View style={styles.box}><Text>3</Text></View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    backgroundColor: '#f0f0f0',
  },
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'skyblue',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default App;
* Breakdown :

*  flex: 1 → Takes up the full screen.
*  flexDirection: 'row' → Places items in a row.
*  justifyContent: 'space-between' → Evenly spaces items.
*  alignItems: 'center' → Centers items vertically.


Which Properties Should You Use?
Use Case Recommended Properties
Stack items horizontally flexDirection: 'row'
Center an item inside a container justifyContent: 'center', alignItems: 'center'
Distribute items evenly justifyContent: 'space-between' or space-around
Allow items to wrap flexWrap: 'wrap'
Apply spacing between elements gap, rowGap, columnGap