The screens from the last two sections each answer a URL, but nothing on screen leads to them yet. Link is the component that turns a tap into navigation in Expo Router, on Android, iOS and the web. Its defaults decide what wraps your content and where the back button returns, so they're worth knowing before you put one on a screen.
Key terms
- Link: the component exported by
expo-routerthat navigates to itshrefwhen pressed. - href: the required
Linkprop that names the destination, as a path string or an object with apathnameand optionalparams. - asChild: a boolean
Linkprop that forwards all ofLink's props,hrefincluded, to its single child. - push: a
Linkprop that always pushes a new route, even the one already on screen. - replace: a
Linkprop that removes the current route from the history and puts thehrefin its place.
Link renders pressable text
Import Link from expo-router and give it the path a route answers. The file src/app/settings/account.tsx answers /settings/account, so this line opens it:
tsx<Link href="/settings/account">Account</Link>
href is required and typed as Href. A Link whose href is null or undefined throws Link: href is required as it renders. On the web, the same component renders an <a> tag and performs a client-side navigation.
By default, Link renders its children inside a React Native Text. That suits a label. A View or a Pressable placed inside a Link can lay out unexpectedly, though, since it now sits inside a Text.
asChild hands the press to your component
Rows, cards and custom buttons need their own layout, so Link has asChild. With it, Link renders no Text of its own. It forwards all of its props to its first child, href and role included, and the onPress that performs the navigation goes along with them. The child must accept onPress or onClick, which Pressable does.
asChild expects exactly one child element. Give it two siblings and Link throws, asking for a single child that will emit the onPress event. Put everything the row shows inside one Pressable and pass that.
You can still give a Link your own onPress. Link calls it first and then navigates, which makes it a good place to record the tap.
Relative hrefs
An href that starts with ./ (the current directory) or ../ (the parent directory) is resolved against the screen that is currently rendered. Only hrefs starting with a dot count as relative. From the screen at /settings/profile:
./privacyresolves to/settings/privacy, a sibling in the same folder.../helpresolves to/help, one level up.
By default a relative path is resolved relative to the document, the way a browser treats a page inside its folder. The relativeToDirectory prop resolves it relative to the directory instead, by treating the current route as a folder. From /settings/profile, <Link relativeToDirectory href="./privacy"> goes to /settings/profile/privacy.
One pair of hrefs breaks the pattern. An href of exactly .. or ../ goes back instead of navigating to the parent route, while ../help navigates as usual.
How a Link changes the history
A plain Link sends the same navigation action as router.navigate, which the next lesson takes apart. Two boolean props choose a different one:
pushalways pushes a new route, and never pops or replaces. You can push the route you are on several times, or with new params.replaceremoves the current route from the history and replaces it with the href, which suits redirects.
If a Link sets both props, replace wins over push.
The profile settings screen
The ProfileSettingsScreen example is the screen at src/app/settings/profile.tsx, and it uses each idea once.
The Account row is a Pressable with a row style and two lines of text, wrapped in <Link href="/settings/account" asChild>. The Pressable keeps its layout because nothing wraps it in a Text, and it receives the onPress from Link. Privacy and Help are relative, so from this screen they open /settings/privacy and /help.
The Sign out link is <Link replace href="/sign-in">. The sign-in screen takes the place of the profile settings screen in the history, so pressing back on the sign-in screen won't return to the settings the user just signed out of.
Code Examples
import { Link } from 'expo-router';
import { Pressable, StyleSheet, Text, View } from 'react-native';
export default function ProfileSettingsScreen() {
return (
<View style={styles.screen}>
<Link href="/settings/account" asChild>
<Pressable style={styles.row}>
<Text style={styles.rowTitle}>Account</Text>
<Text style={styles.rowDetail}>Email and password</Text>
</Pressable>
</Link>
<Link href="./privacy" style={styles.link}>
Privacy
</Link>
<Link href="../help" style={styles.link}>
Help
</Link>
<Link replace href="/sign-in" style={styles.link}>
Sign out
</Link>
</View>
);
}
const styles = StyleSheet.create({
screen: { flex: 1, padding: 16, gap: 12 },
row: {
flexDirection: 'row',
justifyContent: 'space-between',
padding: 12,
borderRadius: 8,
backgroundColor: '#f2f2f2',
},
rowTitle: { fontSize: 16, fontWeight: '600' },
rowDetail: { fontSize: 14, color: '#666666' },
link: { fontSize: 16, color: '#2563eb' },
});