Skip to content
expo-router logo

Expo Router

expo-router

Use when implementing file-based routing in Expo with Expo Router. Covers app directory structure, navigation, layouts, dynamic routes, and deep linking.

TheBushidoCollective/han0installs199stars

SKILL.md

Full skill instructions

Expo Router

Use this skill when implementing file-based routing with Expo Router, the recommended navigation solution for Expo apps.

Key Concepts

File-Based Routing

Routes are defined by file structure:

app/
  _layout.tsx          # Root layout
  index.tsx            # / route
  about.tsx            # /​about route
  (tabs)/              # Group (not in URL)
    _layout.tsx        # Tabs layout
    home.tsx           # /​home
    profile.tsx        # /​profile
  users/
    [id].tsx           # /​users/:id dynamic route
    index.tsx          # /​users route

Basic Routes

// app/​index.tsx
import { View, Text } from 'react-native';
import { Link } from 'expo-router';

export default function Home() {
  return (
    <View>
      <Text>Home Screen</​Text>
      <Link href="/​about">Go to About</​Link>
    </​View>
  );
}

// app/​about.tsx
export default function About() {
  return (
    <View>
      <Text>About Screen</​Text>
    </​View>
  );
}

Layouts

// app/​_layout.tsx
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <Stack>
      <Stack.Screen name="index" options={{ title: 'Home' }} />
      <Stack.Screen name="about" options={{ title: 'About' }} />
    </​Stack>
  );
}

Tab Navigation

// app/(tabs)/​_layout.tsx
import { Tabs } from 'expo-router';
import { Ionicons } from '@expo/​vector-icons';

export default function TabLayout() {
  return (
    <Tabs>
      <Tabs.Screen
        name="home"
        options={{
          title: 'Home',
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="home" size={size} color={color} />
          ),
        }}
      />
      <Tabs.Screen
        name="profile"
        options={{
          title: 'Profile',
          tabBarIcon: ({ color, size }) => (
            <Ionicons name="person" size={size} color={color} />
          ),
        }}
      />
    </​Tabs>
  );
}

Best Practices

Dynamic Routes

// app/​users/[id].tsx
import { useLocalSearchParams } from 'expo-router';
import { View, Text } from 'react-native';

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

  return (
    <View>
      <Text>User ID: {id}</​Text>
    </​View>
  );
}

// Navigate to /​users/​123
<Link href="/​users/​123">View User</​Link>
// Or
import { router } from 'expo-router';
router.push('/​users/​123');

Programmatic Navigation

import { router } from 'expo-router';

function MyComponent() {
  const handlePress = () => {
    // Navigate to route
    router.push('/​details');

    // Navigate with params
    router.push({
      pathname: '/​users/[id]',
      params: { id: '123' },
    });

    // Replace current route
    router.replace('/​login');

    // Go back
    router.back();
  };

  return <Button title="Navigate" onPress={handlePress} />;
}

Type-Safe Routes

// types/​navigation.ts
export type RootStackParamList = {
  '/': undefined;
  '/​about': undefined;
  '/​users/[id]': { id: string };
  '/​posts/[id]': { id: string; title?: string };
};

// Usage with type safety
import { router } from 'expo-router';
import type { RootStackParamList } from './​types/​navigation';

// TypeScript will enforce correct params
router.push({
  pathname: '/​users/[id]' as const,
  params: { id: '123' },
});

Route Groups

Group routes without affecting URLs:

app/
  (auth)/              # Group (not in URL)
    login.tsx          # /​login
    register.tsx       # /​register
    _layout.tsx        # Auth layout
  (app)/               # Group (not in URL)
    home.tsx           # /​home
    profile.tsx        # /​profile
    _layout.tsx        # App layout
// app/(auth)/​_layout.tsx
import { Stack } from 'expo-router';

export default function AuthLayout() {
  return (
    <Stack screenOptions={{ headerShown: false }}>
      <Stack.Screen name="login" />
      <Stack.Screen name="register" />
    </​Stack>
  );
}

Common Patterns

Authentication Flow

// app/​_layout.tsx
import { Slot, useRouter, useSegments } from 'expo-router';
import { useEffect } from 'react';
import { useAuth } from './​hooks/​useAuth';

export default function RootLayout() {
  const { user, loading } = useAuth();
  const segments = useSegments();
  const router = useRouter();

  useEffect(() => {
    if (loading) return;

    const inAuthGroup = segments[0] === '(auth)';

    if (!user && !inAuthGroup) {
      router.replace('/(auth)/​login');
    } else if (user && inAuthGroup) {
      router.replace('/(app)/​home');
    }
  }, [user, loading, segments]);

  return <Slot />;
}

Modal Routes

// app/​_layout.tsx
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <Stack>
      <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
      <Stack.Screen
        name="modal"
        options={{
          presentation: 'modal',
          title: 'Modal',
        }}
      />
    </​Stack>
  );
}

// app/​modal.tsx
import { View, Text, Button } from 'react-native';
import { router } from 'expo-router';

export default function Modal() {
  return (
    <View>
      <Text>Modal Content</​Text>
      <Button title="Close" onPress={() => router.back()} />
    </​View>
  );
}

Deep Linking

// app.json
{
  "expo": {
    "scheme": "myapp",
    "plugins": ["expo-router"]
  }
}
// Deep link: myapp://users/​123
// Opens app/​users/[id].tsx with id="123"

// Universal link: https://myapp.com/​users/​123
// Requires additional iOS/​Android configuration

Search Params

import { useLocalSearchParams } from 'expo-router';

function ProductScreen() {
  const { id, category, sort } = useLocalSearchParams<{
    id: string;
    category?: string;
    sort?: string;
  }>();

  return (
    <View>
      <Text>Product: {id}</​Text>
      <Text>Category: {category}</​Text>
      <Text>Sort: {sort}</​Text>
    </​View>
  );
}

// Navigate with query params
<Link href="/​products/​123?category=electronics&sort=price">
  View Product
</​Link>

Anti-Patterns

Don't Use React Navigation Directly

// Bad - Mixing Expo Router with React Navigation
import { NavigationContainer } from '@react-navigation/​native';

// Good - Use Expo Router only
import { Stack } from 'expo-router';

Don't Nest Navigators Incorrectly

// Bad - Multiple Stack navigators without layout
// app/​home.tsx
<Stack>
  <Stack.Screen name="details" />
</​Stack>

// Good - Use layouts for nested navigation
// app/​home/​_layout.tsx
<Stack>
  <Stack.Screen name="index" />
  <Stack.Screen name="details" />
</​Stack>

Don't Hardcode Routes

// Bad - String literals everywhere
router.push('/​users/​123');
router.push('/​prodcts/​456'); // Typo won't be caught

// Good - Use constants or typed routes
const ROUTES = {
  USER_DETAILS: (id: string) => `/​users/​${id}` as const,
  PRODUCT_DETAILS: (id: string) => `/​products/​${id}` as const,
} as const;

router.push(ROUTES.USER_DETAILS('123'));

Related Skills

  • expo-config: Configuring deep linking
  • expo-modules: Using navigation with Expo modules

More skills from TheBushidoCollective

typescript-utility-types logo
TheBushidoCollective/han

typescript-utility-types

Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.

199 0
View
shell-portability logo
TheBushidoCollective/han

shell-portability

Use when writing shell scripts that need to run across different systems, shells, or environments. Covers POSIX compatibility and platform differences.

199 0
View
tailwind-performance logo
TheBushidoCollective/han

tailwind-performance

Use when optimizing Tailwind CSS for production, reducing bundle size, and improving performance. Covers PurgeCSS, JIT mode, and build optimization.

199 0
View
figma-generate-component logo
TheBushidoCollective/han

figma-generate-component

Generate production-ready code from a Figma component or frame using the Figma MCP server

199 0
View
dialyzer-integration logo
TheBushidoCollective/han

dialyzer-integration

Use when integrating Dialyzer into development workflows and CI/CD pipelines for Erlang/Elixir projects.

199 0
View
rubocop-configuration logo
TheBushidoCollective/han

rubocop-configuration

Use when ruboCop configuration including YAML config, departments, inheritance, and customization.

199 0
View
elixir-otp-patterns logo
TheBushidoCollective/han

elixir-otp-patterns

Use when Elixir OTP patterns including GenServer, Supervisor, Agent, and Task. Use when building concurrent, fault-tolerant Elixir applications.

199 0
View
ameba-custom-rules logo
TheBushidoCollective/han

ameba-custom-rules

Use when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.

199 0
View
Swift Optionals Patterns logo
TheBushidoCollective/han

Swift Optionals Patterns

Use when swift's optional handling patterns including optional binding, chaining, nil coalescing, and modern approaches to safely working with optional values while avoiding common pitfalls and force unwrapping.

199 0
View
code-annotation-patterns logo
TheBushidoCollective/han

code-annotation-patterns

Use when annotating code with structured metadata, tags, and markers for AI-assisted development workflows. Covers annotation formats, semantic tags, and integration with development tools.

199 0
View
Scala Collections logo
TheBushidoCollective/han

Scala Collections

Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views, parallel collections, and custom collection builders for efficient data processing.

199 0
View
reactflow-custom-nodes logo
TheBushidoCollective/han

reactflow-custom-nodes

Use when creating custom React Flow nodes, edges, and handles. Covers custom node components, resizable nodes, toolbars, and advanced customization.

199 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View