Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Flexbox Layout | Styling and Navigation
React Native Fundamentals

Flexbox Layout

Pyyhkäise näyttääksesi valikon

Flexbox is the primary layout system in React Native, letting you build flexible and responsive user interfaces. With Flexbox, you can control how components are arranged inside their parent container, both vertically and horizontally. The three most important Flexbox properties in React Native are flexDirection, justifyContent, and alignItems.

  • flexDirection determines the main axis of the layout. By default, React Native arranges children in a column (flexDirection: "column"), stacking them vertically. Setting flexDirection: "row" arranges children horizontally in a row;
  • justifyContent aligns children along the main axis. You can use values like "flex-start" (default, items packed at the start), "center" (items centered), or "space-between" (items spread out with space between);
  • alignItems aligns children along the cross axis, perpendicular to the main axis. Common values are "flex-start", "center", and "flex-end".

Understanding these properties gives you precise control over how your components appear and adapt to different screen sizes.

To see Flexbox in action, consider a simple example that shows both row and column layouts. Suppose you want to arrange three colored boxes in a row, centered both vertically and horizontally. You can achieve this with Flexbox properties.

import React from "react";
import { View, StyleSheet } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.box} />
      <View style={[styles.box, styles.box2]} />
      <View style={[styles.box, styles.box3]} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "row", // Arrange children in a row
    justifyContent: "center", // Center children horizontally
    alignItems: "center", // Center children vertically
  },
  box: {
    width: 60,
    height: 60,
    backgroundColor: "skyblue",
    margin: 8,
  },
  box2: {
    backgroundColor: "tomato",
  },
  box3: {
    backgroundColor: "gold",
  },
});

In this example, the container uses flexDirection: "row" to lay out the boxes horizontally. justifyContent: "center" centers the boxes along the main axis (horizontally), and alignItems: "center" centers them along the cross axis (vertically). If you change flexDirection to "column", the boxes will stack vertically instead.

Flexbox makes it easy to switch between layouts and create adaptive designs without changing your component structure. You only need to adjust the Flexbox properties in your styles.

question mark

Which Flexbox property controls the direction of children in a container?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 3. Luku 2
some-alt