Navigation in a React Native app

Screens stacked on screens, tabs that each remember where you were, a drawer, and the one place apps get wrong: signing in and out. Watch the navigator's state change beside the phone.

Level: IntermediateWatch: 15 steps · ~1 minutePush, pop, sign in and outRecall: 3 questions · 8 cardsCompare 4 waysTSX (React Native)

What you'll take away

  • Explain push and pop, and why the screen under the top one never reloads
  • Tell a stack, tabs and a drawer apart, and why each tab keeps its own stack
  • Spot why Back shows the sign-in screen when signing in just calls navigate
  • Give each sign-in state its own screens, so no Back or link reaches private ones

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
Homesigning in by: screens by sign-in state
root stackrootTabs ↓
React Navigation
Home tabHome
Cart tabCart
Profile tabProfile
open the app → HomeOne card on the Home tab's stack. The screen always shows the top card of the stack in front.
Step 1 / 15 · step by step
1screens stacked up to this one
exitBack would show
insigned in?

The picture: a big phone running a made-up shop app, and beside it the navigator's yard, where every stack is a pile of cards: the root stack up the street, and a pile per tab once you're signed in. Each step is one tap or one Back. A model of what React Navigation does, drawn by hand.

Step 4 · Every option at once, on one clock

Compare them all: Signing in and the navigator

Every tile meets three troubles in a row: sign in then Back, sign out from Settings then Back, and a link to Orders while signed out. ✓ means the right screen; ✗ names the screen it slipped to.

Trouble 1 / 3: Sign in, then press Back
just navigatenavigation.navigate("Tabs")right 0 of 0

Use it when Ordinary moves between screens you can come back from

Skip it when Signing in or out: the old screen stays under the new one

A model of what React Navigation 7 does: navigate goes up to the navigator that has the screen, replace swaps only the top screen, reset clears the whole stack. Press any tile to see it large above, with when it fits.

Which navigator for what

NavigatorWhat it doesBack
stackputs each screen on top of the lasttakes the top one off
bottom tabsswitches between the app's main parts, a stack in eachworks inside the tab's own stack
drawera menu sliding out from the edgecloses the drawer first

Signing in swaps the screens

There's no switch navigator any more (it went in React Navigation 5). Each sign-in state gets its own screens: if: useIsSignedIn in the static API, screens chosen by a condition in the dynamic one, Stack.Protected in expo-router. Signing in only changes the state, and the navigator does the rest. Nothing is left underneath to go back to.

When replace or reset is right

replace swaps the top screen, like a loading screen nobody should return to. reset throws the history away, like at the end of onboarding. For signing in and out they leave the private screens registered, so a link can still open them.

In real code

App.tsx
// React Navigation 7, static API: a root stack whose screens depend on the sign-in state, tabs with a stack each.
import { createStaticNavigation, useNavigation } from "@react-navigation/native";
import { createNativeStackNavigator, type NativeStackNavigationProp } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { Button, Text, View } from "react-native";
import { SessionProvider, useIsSignedIn, useIsSignedOut, useSession } from "./session";

function SignInScreen() {
  const { signIn } = useSession();
  // ✗ navigation.navigate("Tabs") here would leave this screen under the tabs, and Back would come back to it.
  // ✓ Change the state only: the root stack swaps its whole set of screens by itself.
  return <Button title="Sign in" onPress={() => signIn("token-from-your-api")} />;
}

function HomeScreen() {
  const navigation = useNavigation<NativeStackNavigationProp<HomeParams>>();
  return <Button title="Mug ฿290" onPress={() => navigation.push("Product", { id: "mug" })} />;
}

function ProductScreen() {
  const navigation = useNavigation<NativeStackNavigationProp<HomeParams>>();
  return <Button title="Reviews" onPress={() => navigation.push("Reviews")} />;
}

const ReviewsScreen = () => <Text>★★★★★</Text>;
const CartScreen = () => <Text>฿540</Text>;
const SettingsScreen = () => <Text>Notifications</Text>;
const OrdersScreen = () => <Text>#1042 ฿580</Text>;

function ProfileScreen() {
  const { signOut } = useSession();
  return (
    <View>
      <Button title="Sign out" onPress={signOut} />
    </View>
  );
}

// Each tab gets a stack of its own: switching tabs leaves every one where it was.
const HomeStack = createNativeStackNavigator({
  screens: {
    Home: HomeScreen,
    Product: { screen: ProductScreen, linking: "product/:id" },
    Reviews: ReviewsScreen,
  },
});
type HomeParams = { Home: undefined; Product: { id: string }; Reviews: undefined };

const Tabs = createBottomTabNavigator({
  screenOptions: { headerShown: false },
  screens: { HomeTab: HomeStack, Cart: CartScreen, Profile: ProfileScreen },
});

// Signed out, only SignIn exists; signed in, only the rest. Back can't reach the other group, and neither can a link.
const RootStack = createNativeStackNavigator({
  groups: {
    SignedIn: {
      if: useIsSignedIn,
      screens: {
        Tabs: { screen: Tabs, options: { headerShown: false } },
        Settings: SettingsScreen,
        Orders: { screen: OrdersScreen, linking: "orders" },
      },
    },
    SignedOut: {
      if: useIsSignedOut,
      screens: { SignIn: SignInScreen },
    },
  },
});

const Navigation = createStaticNavigation(RootStack);

export default function App() {
  return (
    <SessionProvider>
      <Navigation linking={{ enabled: "auto", prefixes: ["myapp://"] }} />
    </SessionProvider>
  );
}
Run: tsc against each library's own published types (react-native and the libraries this file imports); type-checked only, never run on a phone

The static API: a stack per tab inside bottom tabs, and a root stack whose two groups exist only for their sign-in state (`if: useIsSignedIn`). Signing in only changes the state; the navigator swaps the screens. Type-checked against the libraries' own types; there's no phone here.

Common questions

What's the difference between a stack, tabs and a drawer?

A stack is screens on top of screens: go deeper, then Back one at a time. Tabs are the bar at the bottom that switches between the app's main parts, each holding its own stack. A drawer is a menu that slides out from the edge, for longer lists of places.

Why does Back go to the sign-in screen after I signed in?

Because the app called navigate after signing in: the new screen went on top, and the sign-in screen is still underneath. Give each sign-in state its own screens, or at least reset the whole stack.

Where did the switch navigator go?

It went in React Navigation 5. Instead, the screens are chosen by a condition: if: useIsSignedIn in the static API, screens picked by sign-in state in the dynamic API, and Stack.Protected in expo-router. Old samples with createSwitchNavigator don't work with today's versions.

Read: signing in swaps the screens

When do I use replace, and when reset?

replace swaps only the top screen: good for a loading screen nobody should come back to. reset throws the whole history away: good for the end of onboarding. Either can do sign-in, but the private screens stay in the navigator, so a link can still open them.

What happens when a link opens a private screen while I'm signed out?

If the screen is registered in the navigator, the link simply opens it. With screens per sign-in state, it doesn't exist while signed out, so the user stays on the sign-in screen.

Does switching tabs lose the screen I had open?

No. Each tab holds its own stack, so you come back to the same screen. Tap the tab you're already on, and React Navigation's stack takes you back to that tab's first screen.

Next lesson

Deep links vs Universal Links / App LinksLevel: Intermediate

Screens open from taps inside the app now; next, a link from outside opening one of them: myapp:// vs https://.