Thuta Learning
React Native
BasicMobile DevelopmentIntermediate

Layout with Flexbox

React NativeLesson 620 min

What you'll walk away with

  • Explain the React and native-platform behavior of Layout with Flexbox
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

Layout with Flexbox, in plain language

In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).

Problem → Why → Solution

The problem — why this concept matters

Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)

A common mistake

Relying only on fixed width/height and ignoring flex: 1 — layout can break on devices with different screen sizes

Core concept — understand how it works

Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.

Mental model
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Explain the React and native-platform behavior of Layout with Flexbox

Core syntax or pattern

javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // use the remaining space
    justifyContent: 'center',
    alignItems: 'center',
  },
});

Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.

Read the code step by step

  1. JavaScript stateconst styles = StyleSheet.create({

    In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).

  2. native component and layout},

    Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.

  3. Android or iOS UI});

    Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Execution flow
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Success

Explain the React and native-platform behavior of Layout with Flexbox

Failure

Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)

Let's think about this for a moment

In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).

Let's connect this to a real scenario

To build a header bar (logo on the left, menu icon on the right), just add flexDirection: 'row', justifyContent: 'space-between' to the View's style — the two children automatically get pushed apart. Adding flex: 1 to a child component's style makes it take up the available space — if you want responsive behavior across different screen sizes, using flex is better than fixed width/height.

Code Example

javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // use the remaining space
    justifyContent: 'center',
    alignItems: 'center',
  },
});
You should see
You'll be able to build a header bar with flexDirection: row and arrange the logo/icon side by side.

5-Minute Try-It

Put 3 boxes inside a View with flexDirection: 'row' and try justifyContent with 'flex-start', 'center', and 'space-between'.

A Quick Heads-Up

flex: 1 might not work if the parent container doesn't have a defined height (the root View needs flex: 1 too) — pay attention to the parent-child flex chain.

Real-world usage

How to apply it in a real project

Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Mini challenge

Build it yourself

Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // use the remaining space
    justifyContent: 'center',
    alignItems: 'center',
  },
});

Expected: Explain the React and native-platform behavior of Layout with Flexbox

Show hint

Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Quick quiz 1 / 3

What best describes the core idea of Layout with Flexbox?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • JavaScript state → native component and layout → Android or iOS UI
  • Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.
  • Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)
  • Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

React Native Documentation — React Native

Easy traps

  • Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)
  • Relying only on fixed width/height and ignoring flex: 1 — layout can break on devices with different screen sizes
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

You'll know it worked when: You'll be able to build a header bar with flexDirection: row and arrange the logo/icon side by side.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Layout with Flexbox | Thuta Learning