Some screens only make sense for some users: an order history needs an account, and a sign-in form is pointless once you have one. Expo Router handles this in the layout, by wrapping screens in a guard that redirects anyone it blocks. Where that redirect lands, and what a guard leaves open on the web, decide whether the setup is safe to ship.

Key terms

  • Stack.Protected: a component on Stack that wraps Stack.Screen elements and blocks client-side navigation to them while its guard is false.
  • guard: the required boolean prop of Protected, which keeps the screens inside reachable only while it is true.
  • Tabs.Protected: the same component on the JavaScript Tabs navigator imported from expo-router/js-tabs.

A guard around screens

Protected takes three props: a required boolean guard, an optional redirectTo, and the screens it controls as children. Wrap the screens that depend on a condition:

tsx
<Stack.Protected guard={isSignedIn}>
  <Stack.Screen name="orders" />
</Stack.Protected>

While guard is false, a user who navigates to /orders is redirected. So is a user who is already on that screen, or on a screen nested under it, at the moment the guard turns false. When a guard changes from true to false, every history entry of the screens it guards is also removed from the navigation history, so back can't return to them.

In SDK 58 a guarded route stays registered in its navigator. When its guard fails, the route renders a redirect instead of being removed from the route tree. Guarded screens also get the hidden: true option, which keeps them out of a tab bar.

Declare each screen once. A screen can be in only one active route group at a time, so when its availability depends on logic, wrap it in a Protected group instead of declaring it a second time. Outside production, a layout that declares the same screen name twice throws an error that starts with Screen names must be unique.

The default redirect

Without redirectTo, Expo Router picks the destination from the navigator itself:

  1. The navigator's anchor, the route set with unstable_settings.anchor, when one is set and it isn't guarded.
  2. Otherwise, the first child route that isn't guarded.

"First" follows Expo Router's route sorting, whatever order your Stack.Screen elements are in. index and group routes such as (app) sort before other static routes, shorter static names come before longer ones, and dynamic routes such as [id] come after all static ones.

The ShopLayout example has no anchor. With isSignedIn set to false, index, orders and admin are guarded, which leaves help and sign-in. help is the shorter name, so a signed-out user who opens the app lands on the help screen, even though sign-in is declared first. Setting redirectTo on the guard overrides this choice.

If every route in a navigator is guarded, there is nowhere to send the user. The guarded screen renders nothing, and in development Expo Router logs All routes in this navigator are protected. Ensure at least one route is accessible. It doesn't throw.

Guards inside guards

Protected elements nest, so you can build rules in layers. A screen is guarded when any Protected around it fails: an outer guard that fails excludes everything inside it, whatever the inner guards say. In ShopLayout, admin sits inside guard={isAdmin}, which sits inside guard={isSignedIn}, so only a signed-in admin reaches it. orders is inside the outer guard only, so any signed-in user reaches it.

Guards on tabs

Protected routes also work with the Tabs and Drawer navigators. Tabs from expo-router/js-tabs has a Tabs.Protected member with the same props:

tsx
<Tabs>
  <Tabs.Screen name="index" />
  <Tabs.Protected guard={isSignedIn}>
    <Tabs.Screen name="library" />
  </Tabs.Protected>
</Tabs>

Since guarded screens get hidden: true, the library tab button stays out of the tab bar while the guard fails. NativeTabs has no Protected member: its only static members are Trigger and BottomAccessory.

What stays reachable on the web

Guards are evaluated only on the client. Static site generation creates no HTML file for a protected route, but a user who knows the URL can still request the route's HTML or JavaScript files directly. Client-side navigation, on native or with Link in a web app, doesn't pass through server middleware either. A guard only decides which screen the app shows. It doesn't replace server-side authentication or access control. Keep private data behind an API that checks the session on every request.

Code Examples

tsx
import { Stack } from 'expo-router';

const isSignedIn = false;
const isAdmin = false;

export default function ShopLayout() {
  return (
    <Stack>
      <Stack.Protected guard={isSignedIn}>
        <Stack.Screen name="index" />
        <Stack.Screen name="orders" />

        <Stack.Protected guard={isAdmin}>
          <Stack.Screen name="admin" />
        </Stack.Protected>
      </Stack.Protected>

      <Stack.Protected guard={!isSignedIn}>
        <Stack.Screen name="sign-in" />
      </Stack.Protected>

      <Stack.Screen name="help" />
    </Stack>
  );
}
✓ Completed