React Native screen layout

You can build phone screens with native components and layout rules, giving the Expo app a usable starting point instead of a web page.

Learning outcomes

  • Use React Native core components to build screens without HTML or the browser DOM.
  • Apply StyleSheet and flexbox rules to arrange content and handle long text on phone-sized screens.
  • Account for safe areas and the on-screen keyboard when positioning screen content.
  • Adapt styles and interactions to differences between Android and iOS.

You already know how to build a React page with components, props, state and hooks. Now you need to build the customer-facing app that runs on Android and iOS. The first screens in the Expo app include Log in, Restaurants, Orders and Cart, but they cannot use HTML elements or browser CSS. The layout must also account for the phone's status bar, home indicator and on-screen keyboard.

React Native rendering

React Native keeps React's programming model. Components, props, state, hooks and effects work in familiar ways. What changes is where components render.

On the web, React renders HTML elements into the browser DOM. CSS controls their appearance and layout. React Native renders native platform views instead. There is no DOM, HTML or CSS.

This affects the components you use, how you style them and how you arrange content. You can reuse much of your React knowledge, but you need to use React Native's component and layout model.

Core components

React Native provides core components that replace the HTML elements you use every day. They come from the react-native package.

React on the webReact NativePurpose
divViewGroups and lays out content
span, p, headingsTextRenders text
buttonPressableHandles press interactions
input, textareaTextInputAccepts text input
imgImageDisplays images

The most important difference is that every visible string must be inside a Text component. A View can contain other views and text components, but it cannot render a raw string.

On the web, this is valid:

tsx
<div>
  Welcome back
  <span>Priya</span>
</div>

In React Native, put both strings inside Text:

tsx
import { Text, View } from "react-native";

export default function Welcome() {
  return (
    <View>
      <Text>Welcome back</Text>
      <Text>Priya</Text>
    </View>
  );
}

You can nest Text components when you need different formatting within one sentence. The inner component inherits text styles from its parent.

tsx
<Text>
  Welcome back, <Text style={{ fontWeight: "bold" }}>Priya</Text>
</Text>

This rule also applies to conditional rendering. A condition that evaluates to a string can produce an invalid child.

Press interactions

Use Pressable when a user needs to tap something. It gives you an onPress callback and access to the current press state.

tsx
import { Pressable, Text } from "react-native";

function ContinueButton({ onContinue }: { onContinue: () => void }) {
  return (
    <Pressable
      onPress={onContinue}
      style={({ pressed }) => ({
        padding: 14,
        opacity: pressed ? 0.7 : 1,
      })}
    >
      <Text>Continue</Text>
    </Pressable>
  );
}

Notice that the event is called onPress, not onClick. The style callback receives the press state, so you can give immediate visual feedback without managing another piece of component state.

Older React Native code often uses TouchableOpacity, which fades its children when pressed. Prefer Pressable in new screens because it exposes the interaction state and lets you choose the visual feedback.

Text input

TextInput replaces the HTML input and textarea elements. It works as a controlled component, but its change handler has a different signature.

On the web, you commonly read the new value through event.target.value. React Native's onChangeText passes the new string directly.

tsx
import { useState } from "react";
import { TextInput } from "react-native";

function EmailInput() {
  const [email, setEmail] = useState("");

  return (
    <TextInput
      value={email}
      onChangeText={setEmail}
      placeholder="Email address"
      keyboardType="email-address"
      autoCapitalize="none"
      autoCorrect={false}
    />
  );
}

Several props matter when you build authentication screens:

PropPurpose
secureTextEntryHides password characters
keyboardType="email-address"Opens a keyboard suited to email input
autoCapitalize="none"Prevents automatic capitalization
autoCorrect={false}Prevents autocorrect from changing input
autoComplete="email"Lets the OS offer saved email credentials
autoComplete="current-password"Lets the OS offer saved passwords

These props influence the phone's keyboard and autofill behavior. They are not CSS settings.

Images and scrolling

An Image can display a remote URL. Give it explicit dimensions so the layout knows how much space to reserve before the image downloads.

tsx
import { Image } from "react-native";

<Image
  source={{ uri: "https://example.com/pizza.jpg" }}
  style={{ width: 120, height: 120 }}
  resizeMode="cover"
/>

resizeMode controls how the image fits its dimensions. cover fills the box and may crop the image. contain fits the entire image inside the box. stretch scales it to fill the box.

For scrollable content, choose between ScrollView and FlatList based on the amount of data.

  • ScrollView renders all its children and allows the content to scroll. It works well for a login form or a short screen.
  • FlatList virtualizes a long list, rendering the items around the visible area rather than mounting every row at once.

The restaurant list can eventually contain many records. Rendering every restaurant at once would create unnecessary work on a phone. You will use FlatList when you build that screen.

Styling with JavaScript objects

React Native has no CSS files, class names or selectors. Styles are JavaScript objects passed through the style prop.

For styles used repeatedly, define them with StyleSheet.create:

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

export default function RestaurantHeading() {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Restaurants near you</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: "#ffffff",
    padding: 16,
    borderRadius: 12,
  },
  title: {
    fontSize: 20,
    fontWeight: "bold",
  },
});

StyleSheet.create gives TypeScript a typed style object. An invalid property such as paddng produces a type error. Inline style objects also work and are useful for one-off values.

Style arrays

The style prop accepts an array of styles. Later entries override earlier properties when they conflict. React Native skips false, null and undefined entries.

This is useful when a component changes appearance based on its state:

tsx
<View style={[styles.pill, isOpen && styles.open]}>
  <Text>{isOpen ? "Open" : "Closed"}</Text>
</View>

The base style always applies. The open style applies only when isOpen is true.

CSS differences

Some habits from web CSS do not transfer directly.

No cascade. A style on a View does not automatically reach its children. Setting color: "red" on a View does not make its text red. Apply text-related styles to the Text component.

Limited inheritance. Nested Text components inherit text styles from their parent. Other component types do not inherit styles this way.

No selectors or media queries. Give each component its style directly. For responsive layouts, use useWindowDimensions, which updates when the screen dimensions change, including on rotation.

tsx
import { useWindowDimensions, View } from "react-native";

function ResponsiveContainer() {
  const { width } = useWindowDimensions();

  return <View style={{ padding: width > 600 ? 32 : 16 }} />;
}

Density-independent dimensions. Most numeric dimensions use density-independent pixels, or dp. A value such as padding: 16 is scaled by the operating system according to the device's pixel density. This helps the physical size remain consistent across phones.

Percentages also work for dimensions such as width: "50%", but CSS units such as rem and px are not generally used for numeric layout properties.

Flexbox layout

React Native uses flexbox for layout, but its defaults differ from the web. A View is already a flex container. You do not need display: "flex".

The defaults that matter most are:

PropertyReact Native defaultTypical web default
flexDirection"column""row" for a flex container
flexShrink01
alignContent"flex-start""stretch"
position"relative""static"

The default column direction means that children stack vertically. The default flexShrink: 0 means they do not shrink to fit a smaller available space.

These differences explain why a layout copied from a web application can look completely different on a phone.

A restaurant row containing an image, name and Open pill. The web defaults place all three in a row and allow the name to shrink. React Native defaults stack them in a column. Setting flexDirection to row, flex to 1 on the name container and numberOfLines to 1 on the name produces a row with a truncated name.A restaurant row containing an image, name and Open pill. The web defaults place all three in a row and allow the name to shrink. React Native defaults stack them in a column. Setting flexDirection to row, flex to 1 on the name container and numberOfLines to 1 on the name produces a row with a truncated name.

A restaurant row

Consider a restaurant row with a thumbnail, a restaurant name and an Open or Closed pill. You want the image on the left, the name in the middle and the status on the right.

A row direction handles the basic arrangement:

tsx
<View style={{ flexDirection: "row", alignItems: "center", gap: 12 }}>
  <Image source={{ uri: imageUrl }} style={{ width: 72, height: 72 }} />
  <Text>{name}</Text>
  <Text>{isOpen ? "Open" : "Closed"}</Text>
</View>

There is another problem. Restaurant names can be long. A name such as "Classic Indian Kitchen and Family Restaurant" could push the status pill outside the screen.

Give the name a containing View with flex: 1. This lets it take the available space after the image and pill have been laid out.

tsx
<View style={{ flexDirection: "row", alignItems: "center", gap: 12 }}>
  <Image source={{ uri: imageUrl }} style={{ width: 72, height: 72 }} />
  <View style={{ flex: 1 }}>
    <Text numberOfLines={1}>{name}</Text>
  </View>
  <Text>{isOpen ? "Open" : "Closed"}</Text>
</View>

Three properties control the result:

  1. flexDirection: "row" places the children side by side.
  2. flex: 1 on the middle container gives it the remaining available space.
  3. numberOfLines={1} truncates the name when it does not fit.

In React Native, flex: 1 is a shorthand that allows a component to grow and shrink, starting from a zero basis. It is not equivalent to width: "100%". It is commonly used when a component should occupy the remaining space.

You will reuse this layout pattern for menu items and order rows.

Safe areas

A phone screen includes areas that should not contain ordinary screen content. These include the status bar, camera cutouts, rounded corners and the home indicator.

The safe area is the usable region that avoids these areas. Its dimensions vary across devices and orientations.

React Native screens use react-native-safe-area-context to read and apply safe-area insets. Expo Router already mounts a SafeAreaProvider at the root, so you can use the package's consumer components and hooks without adding another provider.

For a screen without a navigation header, SafeAreaView can apply the insets automatically:

tsx
import { Text } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";

function Screen() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <Text>Welcome</Text>
    </SafeAreaView>
  );
}

Sometimes you need control over individual edges. For example, a screen may already have a header and only need additional bottom padding.

Use useSafeAreaInsets to read the inset values in dp:

tsx
import { View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

function Screen() {
  const insets = useSafeAreaInsets();

  return <View style={{ flex: 1, paddingBottom: insets.bottom }} />;
}

A screen inside a Stack with a visible header does not need to apply another top inset. The navigation header already sits below the status bar. The same principle applies to the tab bar at the bottom.

Keyboard handling

On the web, a mobile browser generally resizes its visible page when the keyboard opens. In React Native, the keyboard can cover the bottom of your screen.

This matters on a login screen. An email field and password field may fit before the keyboard opens, but the login button can disappear behind it.

KeyboardAvoidingView adjusts its content when the keyboard appears. A ScrollView lets the user reach fields and buttons when the remaining space is too small.

tsx
import { KeyboardAvoidingView, Platform, ScrollView } from "react-native";

function LoginScreen() {
  return (
    <KeyboardAvoidingView
      style={{ flex: 1 }}
      behavior={Platform.OS === "ios" ? "padding" : "height"}
    >
      <ScrollView
        contentContainerStyle={{ flexGrow: 1 }}
        keyboardShouldPersistTaps="handled"
      >
        {/* Login fields and button */}
      </ScrollView>
    </KeyboardAvoidingView>
  );
}

The two components solve different problems. KeyboardAvoidingView adjusts the available layout when the keyboard appears. ScrollView allows the user to reach content that no longer fits.

Pay attention to these details:

  • On iOS, behavior="padding" adds bottom padding based on the keyboard height.
  • On Android, behavior="height" often works. Android can also resize the window itself, so test the layout without a behavior setting if it looks wrong.
  • keyboardVerticalOffset accounts for content above the avoiding view, such as a Stack header. Without the correct offset, the form may stop short of the available space.
  • keyboardShouldPersistTaps="handled" lets a tap on a child control register while the keyboard is open. Without it, the first tap may only dismiss the keyboard.

Do not assume that a layout which works in the emulator also works on every physical device. Keyboard dimensions and system behavior vary.

Platform differences

Most React Native components work across Android and iOS. Some platform-specific styling still requires different properties.

Shadows are a common example. iOS uses shadow properties such as shadowColor, shadowOpacity and shadowRadius. Android commonly uses elevation.

You can use Platform.select to choose values for each platform:

tsx
import { Platform, StyleSheet } from "react-native";

const styles = StyleSheet.create({
  card: {
    backgroundColor: "#ffffff",
    ...Platform.select({
      ios: {
        shadowColor: "#000000",
        shadowOpacity: 0.12,
        shadowRadius: 8,
      },
      android: {
        elevation: 4,
      },
    }),
  },
});

You can also check Platform.OS when component behavior needs to change:

tsx
import { Platform } from "react-native";

const behavior = Platform.OS === "ios" ? "padding" : "height";

Android also has a system back button or gesture. Expo Router connects it to navigation, so it normally pops the current screen. Test modals and custom navigation flows on Android to confirm that the back action matches what the user expects.

Accessibility and development feedback

Native screens need to work with touch input and screen readers. React Native provides accessibility props directly on its components.

For a Pressable, accessibilityRole describes the control and accessibilityLabel provides a readable name. hitSlop expands the tappable area without changing the visible size.

tsx
<Pressable
  hitSlop={12}
  accessibilityRole="button"
  accessibilityLabel="Close"
  onPress={onClose}
>
  <Text>Close</Text>
</Pressable>

Touch targets should be large enough to tap reliably. A small icon can have an expanded hit area without making the icon itself visually larger.

During development, you can use several tools to inspect what is happening:

  • console.log, console.warn and console.error work as expected. Their output appears in the terminal running npx expo start.
  • Press m in the terminal, shake the phone or press Cmd+M in the Android emulator to open the development menu. It includes reload and inspection tools.
  • Press r in the terminal to reload the app.
  • Press j in the terminal to open React Native DevTools, where you can inspect the component tree, props and state and use breakpoints.

Saving a component file triggers Fast Refresh. It attempts to preserve component state when the component code changes, so you can iterate without restarting the entire app after every edit.

Applying native layout to the Expo app

The Expo app starts with screens for Log in, Create account, Restaurants, Orders and Cart. Each screen needs native components rather than HTML elements.

The View, Text and Pressable components provide the building blocks. StyleSheet.create keeps repeated styling in one place, while React Native flexbox controls how the components fit together. Safe-area handling protects content from the phone's system UI.

The authentication screens also need room for the keyboard. The form fields and submit button must remain reachable when the keyboard is visible. These same layout rules will apply when the app later displays restaurant rows, menus and order details.

In the Expo app shell exercise, you will create the initial route screens and run them on an emulator and a phone. Focus on how the components behave on a real screen before adding API integration.

Check your understanding

Question 1 of 4

A developer copies this conditional rendering from a web component:

tsx
<View>
  {restaurants.length === 0 && "No restaurants found"}
</View>

The app throws a text-rendering error when the list is empty. What is the correct explanation?

Pick an answer, then say how sure you are.

References

Everything above is explained on this page; these are where it comes from (React Native 0.88, Expo SDK 58).

Finished Lesson 4.1?