With Expo Router you don't register screens in a navigator config. You add a file under src/app, and the app gets a page whose URL is the file's path. Reading that one folder tells you every URL the app answers, both in the browser's address bar on the web and as a deep link into the native app.

Key terms

  • src/app: the directory Expo Router builds its routes from, where every file except the special _layout files default-exports a page.
  • Static route: a route whose file and directory names use no special notation, so its URL is exactly its path in the file tree.
  • index.tsx: the file that acts as the default route of its directory.

The path is the URL

Take a reading tracker whose src/app folder looks like this:

text
src/app/
  _layout.tsx
  index.tsx
  search.tsx
  shelf/
    index.tsx
    wishlist.tsx

Each file here except _layout.tsx has a default export, a React component, and that component is one page of the app. search.tsx answers /search, so the same page opens at yourapp.com/search in a browser and at yourapp://search in the native app. shelf/wishlist.tsx answers /shelf/wishlist. None of these names use any special notation, which makes them static routes: the URL follows the folders one segment at a time, and the shelf directory holds the screens that belong together.

_layout.tsx is the exception. Layout files are not pages. They define how the routes in their directory relate to each other, and the next section of the course is about them.

A route file needs its default export. Without one, Expo Router can log a development warning that the file "is missing the required default export". Two files that land on the same route, such as search.tsx and search.js side by side, are an error: outside production, Expo Router throws a message saying the two route files "conflict on the route" and telling you to remove or rename one of them.

A few path names, /assets among them, are reserved by Metro and Expo Router. Don't give a route one of those names.

index.tsx answers its folder's URL

shelf/index.tsx does not answer /shelf/index. An index.tsx file is the default route of its directory, so it answers /shelf. When Expo Router turns a file path into a URL pattern, an index segment becomes an empty segment. That is also why src/app/index.tsx, the index of the whole folder, answers /.

Components, hooks and helpers live elsewhere

src/app is only for routes. Expo Router treats any file inside it as a route, so a reusable book-cover.tsx dropped next to the screens turns into a page at /book-cover. Components, hooks and utilities go in other directories such as src/components, src/hooks and src/constants. Files there have no URL and can't be the target of a navigation action, but any page in src/app can import and render them.

Projects created from the default template on SDK 55 and later start out this way: their top-level src directory already contains app, components, constants and hooks. The config files (app.json, app.config.ts, package.json, metro.config.js, tsconfig.json) stay in the project root, outside src. The template's tsconfig.json maps the @/* alias to ./src/*, so a screen can import shared code by its place under src instead of counting ../ segments.

The ShelfScreen example below the lesson is the page at /shelf, and the component it renders comes from outside the routes folder:

tsx
import BookCover from '@/components/book-cover';

BookCover is the second example. It lives in src/components/book-cover.tsx, so it renders inside the shelf page but has no URL of its own.

Expo CLI uses src/app as the routes root when that directory exists and falls back to a root app directory otherwise. In a project that has both, only src/app is used, and the CLI logs Using src/app as the root directory for Expo Router. when it picks it. If screens you added to a root app folder never show up, check whether a src/app folder exists next to it.

Code Examples

tsx
import { ScrollView } from 'react-native';

import BookCover from '@/components/book-cover';

// src/app/shelf/index.tsx answers /shelf
export default function ShelfScreen() {
  return (
    <ScrollView>
      <BookCover title="The Left Hand of Darkness" pages={304} />
      <BookCover title="Piranesi" pages={272} />
    </ScrollView>
  );
}
tsx
import { Text, View } from 'react-native';

type BookCoverProps = {
  title: string;
  pages: number;
};

// src/components/book-cover.tsx: outside src/app, so it has no URL
export default function BookCover({ title, pages }: BookCoverProps) {
  return (
    <View>
      <Text>{title}</Text>
      <Text>{pages} pages</Text>
    </View>
  );
}
✓ Completed