Layout
Place components in rows and columns and centre them.
Your React Native code runs inside your browser tab on React Native for Web, shown on a phone-sized screen. Nothing to install.
Columns come first
A View lays its children out with flexbox. Unlike a web page, the direction is a column unless you say otherwise: children are stacked from top to bottom.
gap puts space between the children.
import { Text, View } from "react-native";
export default function App() {
return (
<View style={{ gap: 12 }}>
<Text>First</Text>
<Text>Second</Text>
<Text>Third</Text>
</View>
);
}Rows
flexDirection: "row" lays the children out from left to right instead.
The direction belongs to the View that holds the children, not to the children.
import { Text, View } from "react-native";
export default function App() {
return (
<View style={{ flexDirection: "row", gap: 12 }}>
<Text>Home</Text>
<Text>Orders</Text>
<Text>Profile</Text>
</View>
);
}Your turn: a row
Put the direction on the container.
Practice task
Show the three letters A, B and C side by side in a row, each in its own Text.
Your answer is run and checked in the lesson. Create a free account to try it.
Centring
justifyContent places children along the direction of the View, and alignItems places them across it. Both accept center.
flex: 1 makes a View grow to fill the space it is given. Without it, a View is only as tall as its contents, so there is nothing to centre within.
import { Text, View } from "react-native";
export default function App() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Right in the middle</Text>
</View>
);
}Your turn: centre a message
Three properties on one View.
Practice task
Show the word Loading in the middle of the screen, centred both ways. The View needs flex 1, justifyContent center and alignItems center.
Your answer is run and checked in the lesson. Create a free account to try it.