Some navigation has no link to tap: an order is placed, a form saves, a custom back button is pressed. useRouter gives a component a router object it can call from event handlers. Its methods differ in what they do to the stack of screens the user backs out through, and picking the wrong one leaves duplicate screens or lets back return to a screen the user is done with.
Key terms
- useRouter: a hook exported by
expo-routerthat returns the router object, whose methods navigate from code. - router.navigate: the method that sends the same NAVIGATE action as a plain
Link. - router.push: the method that navigates with a push operation, adding a screen.
- router.back: the method that goes back in the navigation history.
- useCanGoBack: a hook exported by
expo-routerthat returns whether the router can go back, and re-renders when that changes.
The router comes from a hook
Call useRouter() inside the component and call its methods from handlers such as a button's onPress. The router has push, navigate, replace, back, canGoBack, setParams and prefetch, plus dismiss, dismissTo, dismissAll and canDismiss, which a later course covers. navigate and push take an href, the same strings and objects a Link accepts, and optional navigation options.
expo-router also exports a module-level router, and older code imports it directly. Inside a component, use the hook. The module-level router throws The imperative router is unavailable before the first render has finished. if its methods run before the first render has finished.
navigate checks the focused screen, push doesn't
With a Stack layout, navigating to a new route pushes a screen and backing out pops it. The two methods differ in one check:
router.push(href)adds a new screen on top every time. Pushing/cartwhile the cart is focused leaves two cart screens on the stack.router.navigate(href)first compares the target with the focused screen. When it is the same route, the focused screen is reused and the stack doesn't grow. For any other route, it appends a new screen, likepush.
The navigation guide says navigate can "unwind to an existing route on the stack". Code that relies on this won't work as written. With a stack of home, cart and checkout, router.navigate('/cart') from checkout adds a fourth screen, and the first cart screen stays where it was.
A cart button on every screen
The navigate check matters for controls that appear on many screens, such as a cart button in a header. With router.navigate('/cart'), tapping it on the cart screen reuses that screen. With router.push('/cart'), the same tap stacks a second cart screen, and the user has to press back twice to leave.
replace and back
router.replace(href) navigates without adding to the history. It swaps the focused screen for the new route at the same position, so the number of screens on the stack stays the same.
router.back() goes back in the history. In a stack with more than one screen, it pops exactly one. On the first screen the stack doesn't handle it, and development builds log an error that includes Is there any screen to go back to? (production shows nothing).
router.canGoBack() takes no arguments and returns a boolean: false before the navigation container has mounted, and otherwise whether the root navigation can go back. Call it from an effect or an event handler. Called during render it can return a stale value, and Expo Router warns once and points you to useCanGoBack(). The hook returns the same value and re-renders when it changes. With tabs or a drawer, that value can change once after the first render.
The checkout screen
The CheckoutScreen example uses all four methods.
handlePlaceOrder ends with router.replace('/order-confirmed'). The confirmation takes the checkout screen's place, so back from the confirmation goes to whatever was below checkout and never shows the submitted form again.
"Add a gift note" calls router.push('/gift-note'), a new screen on top that back removes. "Edit cart" calls router.navigate('/cart'). Checkout is focused, so this appends a new cart screen even when the user came from the cart. When the cart sits directly below checkout, router.back() is the call that returns to it.
The Back button renders only when useCanGoBack() returns true, so the first screen of the stack never shows a button that does nothing.
Code Examples
import { useCanGoBack, useRouter } from 'expo-router';
import { Button, StyleSheet, Text, View } from 'react-native';
async function submitOrder() {
// Send the order to your API here.
}
export default function CheckoutScreen() {
const router = useRouter();
const canGoBack = useCanGoBack();
async function handlePlaceOrder() {
await submitOrder();
router.replace('/order-confirmed');
}
return (
<View style={styles.screen}>
<Text style={styles.title}>Checkout</Text>
<Button title="Edit cart" onPress={() => router.navigate('/cart')} />
<Button title="Add a gift note" onPress={() => router.push('/gift-note')} />
<Button title="Place order" onPress={handlePlaceOrder} />
{canGoBack && <Button title="Back" onPress={() => router.back()} />}
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, padding: 16, gap: 12 },
title: { fontSize: 22, fontWeight: '700' },
});