Navigation Architecture

Article · 2:00

File-based routing with Expo Router, and the three navigators every app is built from.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Expo Router puts navigation in the file system: a file at app/settings.tsx is the route /settings, a folder with a _layout.tsx is a navigator, and app/article/[id].tsx is a dynamic route. It is built on React Navigation, so the concepts transfer, but you declare far less of it by hand. The decisive advantage is that every screen has a URL, which is what makes deep links and web support fall out of the structure rather than needing separate wiring.

Three navigators cover nearly everything. A Stack pushes screens onto a pile with a back gesture - the pattern for drilling into detail. Tabs switch between sections that are peers, each keeping its own stack and scroll position. A Drawer slides in from the edge, and is more common on Android than on iOS, where a tab bar is the platform convention.

They nest, and the nesting is the architecture. The usual shape is a root stack holding a tab navigator, so that a modal or a sign-in screen can be pushed over the top of the tabs rather than inside one of them. Groups - a folder in parentheses like (tabs) - organise files without adding a path segment.

TS Example 1 52 lines
// app/_layout.tsx - the root. A stack, with the tabs inside it.
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <Stack>
      {/* The (tabs) group is a folder that adds no URL segment. */}
      <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
      {/* Pushed OVER the tabs, which is why it lives in the root stack. */}
      <Stack.Screen name="sign-in" options={{ presentation: 'modal' }} />
    </Stack>
  );
}

// app/(tabs)/_layout.tsx - two sections that are peers.
import { Tabs } from 'expo-router';

export function TabLayout() {
  return (
    <Tabs screenOptions={{ tabBarActiveTintColor: '#0c7' }}>
      <Tabs.Screen name="index" options={{ title: 'Feed' }} />
      <Tabs.Screen name="profile" options={{ title: 'Profile' }} />
    </Tabs>
  );
}

// app/article/[id].tsx - a dynamic route. The file name IS the URL.
import { useLocalSearchParams, Link, router } from 'expo-router';
import { View, Text, Pressable } from 'react-native';

export default function ArticleScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();

  return (
    <View style={{ padding: 16, gap: 12 }}>
      <Text>Article {id}</Text>

      {/* Declarative navigation. */}
      <Link href="/profile">Go to profile</Link>

      {/* Imperative, for after an action completes. */}
      <Pressable onPress={() => router.push(`/article/${Number(id) + 1}`)}>
        <Text>Next article</Text>
      </Pressable>

      {/* replace, so Back does not return to a screen that is now wrong. */}
      <Pressable onPress={() => router.replace('/')}>
        <Text>Done</Text>
      </Pressable>
    </View>
  );
}

Open this example in the editor and change it. Nothing you type is saved.