Tutorial: Deep Linking into a React Native App with React Navigation: The Cold-Start Race Most Guides Get Wrong

You add deep linking to a React Native app, test it by tapping a link while the app is open, and it works. Then a user taps the same link with the app killed, and the app opens to the home screen — or crashes — while the link payload is silently dropped. The problem is almost never the URL parsing. It is the ordering between native link delivery and the moment your navigation container exists.
This tutorial wires up deep linking in a React Native app using React Navigation 7 and Expo 53 (SDK 53, React Native 0.79). You will end up with a setup that handles both warm starts and cold starts deterministically, converts a raw URL into a typed route, and reports failures instead of swallowing them. The code below is aimed at a managed Expo project; the native-side notes explain where a bare React Native project differs.
The actual failure mode
On Android, when your app is dead and the user taps a link, the intent arrives before — or at the same instant as — your JavaScript bundle finishes loading. On iOS, the NSUserActivity or URL is handed to the running app object early in launch. React Navigation's linking prop has to both register the URL listener and process any initial URL that arrived before JS was ready.
React Navigation does this via NavigationContainer's linking configuration combined with getInitialURL and subscribe. If you subscribe to Linking yourself in a useEffect and also let the container do its own handling, you get double navigation. If your container mounts inside an auth gate that returns null until a session check resolves, the initial URL is consumed before there is a navigator to receive it.
That last case is the one most guides skip.
Step 1: Configure the URL scheme
In app.json for Expo, declare a scheme. Custom schemes are the portable choice; universal links require additional native configuration (associated domains on iOS, assetlinks.json on Android) that is out of scope here.
{
"expo": {
"name": "DeepLinkDemo",
"slug": "deeplink-demo",
"scheme": "deeplinkdemo",
"ios": { "bundleIdentifier": "com.example.deeplinkdemo" },
"android": { "package": "com.example.deeplinkdemo" }
}
}In a bare React Native project you instead add an intent filter to AndroidManifest.xml and a URL type to Info.plist. Keep the scheme lowercase; scheme matching is case-sensitive on Android.
Step 2: Define the linking config
The screens map translates URL paths into route params. Note that path segments are strings at runtime — a numeric id arrives as "42", not 42. Convert explicitly in the component that consumes it.
// navigation/linking.ts
import type { LinkingOptions } from '@react-navigation/native';
import type { RootStackParamList } from './types';
export const linking: LinkingOptions<RootStackParamList> = {
prefixes: ['deeplinkdemo://', 'https://example.com'],
config: {
screens: {
Home: '',
Profile: {
path: 'user/:id',
parse: { id: (value: string) => Number(value) },
},
Settings: 'settings',
NotFound: '*',
},
},
};The parse function in React Navigation runs on the raw string segment, so Number(value) yields a typed param. If the segment is garbage, Number returns NaN — validate in the screen rather than trusting it.
Step 3: Declare the param list
// navigation/types.ts
export type RootStackParamList = {
Home: undefined;
Profile: { id: number };
Settings: undefined;
NotFound: undefined;
};Step 4: Mount the container only after auth resolves
This is where the cold-start bug lives. If you render NavigationContainer before you know whether the user is logged in, the initial URL is delivered to a navigator that will be replaced a moment later. The fix: gate the container's mount on your auth state, and render a splash screen in the meantime.
// App.tsx
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { ActivityIndicator, View, StyleSheet } from 'react-native';
import { linking } from './navigation/linking';
import type { RootStackParamList } from './navigation/types';
import { useSession } from './auth/useSession';
import HomeScreen from './screens/HomeScreen';
import ProfileScreen from './screens/ProfileScreen';
import SettingsScreen from './screens/SettingsScreen';
import NotFoundScreen from './screens/NotFoundScreen';
const Stack = createNativeStackNavigator<RootStackParamList>();
export default function App() {
const { status } = useSession();
if (status === 'loading') {
return (
<View style={styles.splash}>
<ActivityIndicator />
</View>
);
}
return (
<NavigationContainer linking={linking} fallback={<ActivityIndicator />}>
<Stack.Navigator>
{status === 'authenticated' ? (
<>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</>
) : (
<>
<Stack.Screen name="SignIn" component={HomeScreen} />
</>
)}
<Stack.Screen name="NotFound" component={NotFoundScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
splash: { flex: 1, alignItems: 'center', justifyContent: 'center' },
});Keep this gate small. If useSession itself subscribes to Linking, you are back to double handling.
⚠️ Common mistake: rendering
NavigationContainerbehind an earlyreturn nullwhile a session check runs. The container has already calledgetInitialURLby the time it unmounts, so the cold-start link is consumed and discarded. Always gate the mount, not the children.
Step 5: Handle the authenticated-but-unauthorized case
A cold-start link to user/:id while logged out should not crash. Send the user to sign-in and remember where they were going. The clean way is a small pending-route module rather than stuffing the URL into global state.
// navigation/pending.ts
let pending: string | null = null;
export function setPending(url: string | null) { pending = url; }
export function takePending(): string | null {
const p = pending;
pending = null;
return p;
}In useSession, before clearing the loading state on successful sign-in, call Linking.getInitialURL() once more? No — the initial URL was already handed to the container on its first mount. Instead, listen for authentication transitions and re-navigate manually if a pending route exists. This is a deliberate trade-off: you lose the container's automatic routing for that one case, but you keep control of the redirect target.
// screens/SignInScreen.tsx (excerpt)
import { useNavigation } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import type { RootStackParamList } from '../navigation/types';
import { takePending } from '../navigation/pending';
type Nav = NativeStackNavigationProp<RootStackParamList>;
export function afterSignIn(navigation: Nav) {
const pending = takePending();
if (pending) {
navigation.navigate('Profile', { id: Number(new URL(pending).pathname.split('/').pop()) });
} else {
navigation.navigate('Home');
}
}new URL is available in Hermes on React Native 0.79. If your app runs on an older JS engine without it, replace the parse with a regex — do not assume a polyfill is present.
Step 6: Verify warm and cold starts separately
Testing only the warm path is why this bug ships. On Android, use adb shell am start with an intent that includes the data URI while the app is force-stopped:
adb shell am force-stop com.example.deeplinkdemo
adb shell am start -W -a android.intent.action.VIEW \
-d "deeplinkdemo://user/42" com.example.deeplinkdemoOn iOS, the simulator cannot reproduce a true cold start for custom schemes reliably; test warm starts with xcrun simctl openurl booted "deeplinkdemo://user/42" and validate cold starts on a physical device.
Watch the first frame after launch. If you land on Home instead of Profile, the container mounted too early or an auth gate discarded params.
When not to do this
If your links only come from push notifications and the notification handler already carries structured data, a custom URL parser adds a translation layer you do not need. And if you are shipping a web build with the same code, Linking.getInitialURL behaves differently in a browser; check the platform before relying on it.
Summary
- The main cold-start failure is mounting
NavigationContainerbefore the navigator that should receive the link exists; gate the mount, not the children. - Let React Navigation's
linkingprop own initial-URL delivery; a secondLinkingsubscription in your ownuseEffectcauses double navigation. - URL path segments arrive as strings — use
parsein the linking config to type them, and validateNaNin the screen. - Test Android cold starts with
adb shell am start -a android.intent.action.VIEW -d <scheme>afterforce-stop; the simulator won't reproduce iOS cold starts faithfully. - If a link requires authentication, store it and navigate manually after sign-in rather than hoping the container replays it.
