Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Expo Router and Navigation

File-Based Routing in Expo Router

Learn how Expo Router turns files in the app folder into screens: index routes, nested folders, route groups, dynamic segments, not-found screens and typed routes.

In Expo Router, the file system is your route table. A file at app/about.tsx becomes the /about screen on every platform: it is a URL on the web and a screen name on iOS and Android. You never write a list of routes; you create files, and navigation, deep links and web URLs all follow.

The router itself needs the real Expo toolchain, so route files are shown read-only. The live stand-ins implement the same file-to-route rules so you can experiment with them.

The basic rules

Text
app/
  _layout.tsx          layout wrapping everything (not a screen)
  index.tsx            /
  about.tsx            /about
  settings/
    index.tsx          /settings
    profile.tsx        /settings/profile
  (auth)/
    login.tsx          /login        (the group name is not in the URL)
  posts/
    [id].tsx           /posts/42     (dynamic segment)
  [...missing].tsx     any unmatched URL (catch-all)
  +not-found.tsx       shown when nothing matches
  • index.tsx is the default screen of its folder.
  • A folder adds a path segment.
  • A name in parentheses, (auth), is a route group: it organizes files and can have its own layout, but it does not appear in the URL.
  • A name in brackets, [id], is a dynamic segment (Dynamic Routes and Params); [...rest] is a catch-all.
  • Files starting with _ (like _layout) and + (like +not-found, +html) have special roles.
  • Every route file must default-export a React component.
TSX
// app/about.tsx
import { Text, View } from 'react-native';

export default function About() {
  return (
    <View style={{ padding: 24 }}>
      <Text>About this app</Text>
    </View>
  );
}

Files become routes

index.tsx The folder's home
folder/ Adds a URL segment
(group) Organizes, hidden in URL
[id] Dynamic value
1 / 2

Turning files into routes

The stand-in below converts a list of file paths into route patterns using these exact rules. Add your own paths and run it.

JS File paths to route patterns

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Matching a URL

Given those patterns, the router picks the most specific match: static segments beat dynamic ones, which beat catch-alls. This stand-in ranks routes the same way.

JS Which route wins?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

/posts/new matches the static route, not :id, because static segments rank higher.

Index routes and groups

app/index.tsx is the home; app/settings/index.tsx is the home of /settings. Groups let you split the app without changing URLs:

Text
app/
  (marketing)/
    index.tsx          /
    pricing.tsx        /pricing
  (app)/
    dashboard.tsx      /dashboard
    _layout.tsx        a different layout for the signed-in area

Two routes must not resolve to the same URL, for example (a)/index.tsx and (b)/index.tsx both map to /. Expo Router warns about it. Groups are also how you build tab bars (Tabs and Drawer) and protected areas (Links, Redirects and Deep Links).

The not-found screen

TSX
// app/+not-found.tsx
import { Link, Stack } from 'expo-router';
import { View, Text } from 'react-native';

export default function NotFound() {
  return (
    <>
      <Stack.Screen options={{ title: 'Oops!' }} />
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>This screen does not exist.</Text>
        <Link href="/">Go home</Link>
      </View>
    </>
  );
}

It receives unmatched URLs, including broken deep links. A catch-all [...missing].tsx also works but is for cases where you want the unmatched path as a parameter.

Typed routes

Turn on "experiments": { "typedRoutes": true } in the config, and the CLI generates types from your files. Then href="/posts/42" is checked at compile time, and a typo such as /postss/42 is a TypeScript error:

TSX
import { Link } from 'expo-router';

<Link href="/about">About</Link>
<Link href={{ pathname: '/posts/[id]', params: { id: '42' } }}>Post 42</Link>

API routes and more

Besides screens, Expo Router can have server-side API routes (files ending in +api.ts that export GET, POST, ...) when you use server output, and +html.tsx customizes the root HTML for web. These are optional; you can ignore them until needed.

Common mistakes

  • No default export, or exporting a non-component, so the route is blank.
  • Placing helper components inside app/: every file there is a route. Put non-screens in components/.
  • Two files resolving to the same URL because of groups.
  • Forgetting that _layout.tsx is not a screen but a wrapper (Layouts and Stack).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which URL does app/(auth)/login.tsx create?

    Parenthesized folders are route groups and do not appear in the URL.
  2. 2. What does app/posts/[id].tsx match?

    [id] is a dynamic segment; [...rest] would match multiple segments.
  3. 3. Where should reusable non-screen components live?

    Every file in app/ becomes a route, so keep non-screens elsewhere.
  4. 4. Which screen is shown when no route matches?

    +not-found.tsx is the fallback for unmatched URLs.

Summary

  • Files in app/ are routes; index is the folder home; folders add segments.
  • (groups) organize without changing URLs; [param] and [...rest] are dynamic segments.
  • _layout wraps routes, +not-found handles misses; typed routes catch bad links at compile time.
  • Keep non-screen code outside app/.

Next: Layouts and Stack Navigation