Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท Expo Router and Navigation

Links, Redirects and Deep Links

Navigate declaratively with Link, redirect users with Redirect, protect routes with Stack.Protected and open screens from URLs with schemes, universal links and app links.

So far you moved between screens with router.push. This lesson adds the declarative tools: the Link component, redirects, protected routes for signed-in areas, and deep links, URLs that open a specific screen inside your app from outside it (a browser, an email, a notification, a QR code).

All router code is read-only here (it needs a device). The live stand-ins implement the redirect rules and deep-link parsing in plain JavaScript.

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

<Link href="/about">About</Link>
<Link href={{ pathname: '/trails/[id]', params: { id: '7' } }}>Trail 7</Link>
<Link href="/settings" replace>Settings (replace)</Link>
<Link href="https://expo.dev" target="_blank">Expo website</Link>

Link renders a text element by default. To wrap your own pressable, use asChild, which passes the link behavior to the child:

TSX
<Link href="/trails/7" asChild>
  <Pressable style={styles.card}>
    <Text>Open trail</Text>
  </Pressable>
</Link>

On web it renders a real <a>, which is good for accessibility and SEO. Relative hrefs (./reviews, ../) are resolved from the current route. Links also support push (always add a screen) and prefetch.

Redirect

<Redirect> navigates as soon as it renders. It is how you express "if this condition, go elsewhere" in a layout or screen:

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

export default function Index() {
  return <Redirect href="/(tabs)" />;
}

On web, redirects can also be configured in app.json for static output, but inside the app a Redirect component is the tool.

Protecting routes

Signed-in areas should be unreachable for guests, including through deep links. Expo Router has a guard component for this:

TSX
// app/_layout.tsx
import { Stack } from 'expo-router';
import { useAuth } from '../hooks/useAuth';

export default function RootLayout() {
  const { isSignedIn, isLoading } = useAuth();
  if (isLoading) return null;                    // keep the splash screen visible

  return (
    <Stack screenOptions={{ headerShown: false }}>
      <Stack.Protected guard={isSignedIn}>
        <Stack.Screen name="(app)" />
      </Stack.Protected>
      <Stack.Protected guard={!isSignedIn}>
        <Stack.Screen name="sign-in" />
      </Stack.Protected>
    </Stack>
  );
}

When guard is false, those routes are removed from the navigator, so a link to /dashboard falls back to an available route (typically the first screen or +not-found). When the state flips (the user signs in), the router moves to the permitted screens. Older code does the same with a Redirect inside a group layout:

TSX
// app/(app)/_layout.tsx
if (!isSignedIn) return <Redirect href="/sign-in" />;
return <Stack />;

Always remember that a client-side guard is for user experience only. Enforce access on your server; anyone can modify an app.

JS A route guard, modeled

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

No output.

A common refinement is to remember where the user wanted to go and return there after sign-in:

TSX
router.replace({ pathname: '/sign-in', params: { next: '/dashboard/stats' } });
// after success:
router.replace(next ?? '/dashboard');

Validate next: only accept paths that start with a single /, never full external URLs.

A deep link is a URL that opens your app at a screen. There are three kinds:

  1. Custom scheme such as trailnotes://trails/42. Easy: set "scheme": "trailnotes" in app.json. Any app can register the same scheme, so do not use it for sensitive actions.
  2. Universal Links (iOS) and App Links (Android): normal https://example.com/trails/42 URLs that open your app when it is installed, verified by files hosted on your domain (apple-app-site-association, assetlinks.json) and the config fields ios.associatedDomains and android.intentFilters. More secure and shareable, since they fall back to a web page.
  3. Development links such as exp://... used by Expo Go.

With Expo Router there is nothing else to write: the URL path is matched against your files, so trailnotes://trails/42 opens app/trails/[id].tsx with id = "42". Query strings become params.

JSON
{
  "expo": {
    "scheme": "trailnotes",
    "ios": { "associatedDomains": ["applinks:trails.example.com"] },
    "android": {
      "intentFilters": [{
        "action": "VIEW",
        "autoVerify": true,
        "data": [{ "scheme": "https", "host": "trails.example.com", "pathPrefix": "/trails" }],
        "category": ["BROWSABLE", "DEFAULT"]
      }]
    }
  }
}

Test links on a simulator without a phone:

Terminal
npx uri-scheme open "trailnotes://trails/42" --ios
npx uri-scheme open "trailnotes://trails/42" --android
JS From deep link to route

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

No output.

Reading the incoming URL yourself

For links that are not routes (an OAuth callback, a payment return), use expo-linking:

TSX
import * as Linking from 'expo-linking';

const initial = await Linking.getInitialURL();     // the URL that launched the app
const sub = Linking.addEventListener('url', ({ url }) => console.log(url));
const parsed = Linking.parse('trailnotes://trails/42?tab=photos');
// parsed.path = 'trails/42', parsed.queryParams = { tab: 'photos' }
const makeUrl = Linking.createURL('trails/42');     // creates a link that works in Expo Go and builds

Linking.openURL('https://example.com') opens other apps or the browser. Linking.canOpenURL needs the scheme declared on iOS (LSApplicationQueriesSchemes, added through a config plugin).

Common mistakes

  • Trusting a client-side guard as security. Verify on the server.
  • Redirect loops: guest-only and protected routes sending each other back and forth. Resolve loading first (render nothing until auth state is known).
  • Forgetting scheme and then wondering why links do nothing in builds.
  • Testing universal links before the association files are deployed on the domain.
  • Accepting an unchecked next parameter, which enables open redirects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is asChild on Link used for?

    asChild passes navigation behavior to the single child instead of rendering its own text element.
  2. 2. What does Stack.Protected guard={false} do?

    Protected screens are unavailable while the guard is false.
  3. 3. Which setting makes trailnotes://trails/42 a valid deep link?

    The scheme registers the URL prefix; Expo Router then matches the rest against your files.
  4. 4. Why are universal links and app links safer than custom schemes?

    They are verified through files hosted on your domain, while any app can register a custom scheme.

Summary

  • Link navigates declaratively (asChild for custom pressables); Redirect navigates on render.
  • Protect areas with Stack.Protected, and enforce real access control on the server.
  • Deep links map URLs to routes automatically: custom scheme, or verified universal/app links.
  • Use expo-linking for non-route links and always validate incoming URLs and next parameters.

Next: Core Components