Bird
Raised Fist0
React Nativemobile~20 mins

Deep linking basics in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: HomeScreen
This screen welcomes the user and shows a button to navigate to a Details screen. The app supports deep linking so that opening a special URL opens the Details screen directly.
Target UI
-------------------
|     HomeScreen   |
|                  |
|  Welcome!        |
|                  |
|  [Go to Details] |
-------------------
Create two screens: HomeScreen and DetailsScreen
Add a button on HomeScreen to navigate to DetailsScreen
Configure deep linking so that opening the URL myapp://details opens DetailsScreen
Show a simple text on DetailsScreen: 'This is the Details Screen'
Use React Navigation for navigation
Starter Code
React Native
import * as React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Welcome!</Text>
      {/* TODO: Add button to navigate to DetailsScreen */}
    </View>
  );
}

function DetailsScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>This is the Details Screen</Text>
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Details" component={DetailsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
Task 1
Task 2
Solution
React Native
import * as React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();

const linking = {
  prefixes: ['myapp://'],
  config: {
    screens: {
      Home: '',
      Details: 'details'
    }
  }
};

function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Welcome!</Text>
      <Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
    </View>
  );
}

function DetailsScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>This is the Details Screen</Text>
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Details" component={DetailsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

We added a Button on the HomeScreen that navigates to the DetailsScreen when pressed. This lets users move between screens inside the app.

To support deep linking, we defined a linking configuration object. It tells React Navigation to listen for URLs starting with myapp://. The path details maps to the Details screen.

We passed this linking config to NavigationContainer. Now, if the app is opened with the URL myapp://details, it will open the DetailsScreen directly.

This setup helps users open specific parts of the app from outside, like from a browser or another app.

Final Result
Completed Screen
-------------------
|     HomeScreen   |
|                  |
|  Welcome!        |
|                  |
|  [Go to Details] |
-------------------

-------------------
|   DetailsScreen  |
|                  |
|  This is the     |
|  Details Screen  |
|                  |
-------------------
Tapping 'Go to Details' button navigates to DetailsScreen
Opening URL 'myapp://details' opens the app directly on DetailsScreen
Stretch Goal
Add a third screen called ProfileScreen and support deep linking to 'myapp://profile'. Add a button on HomeScreen to navigate to ProfileScreen.
💡 Hint
Add ProfileScreen to the stack navigator and update the linking config with 'Profile: "profile"'. Add a button on HomeScreen that navigates to 'Profile'.

Practice

(1/5)
1. What is the main purpose of deep linking in a React Native app?
easy
A. To create animations between screens
B. To open specific screens in the app from external links
C. To improve app performance by caching data
D. To handle user authentication automatically

Solution

  1. Step 1: Understand deep linking concept

    Deep linking allows apps to open specific screens directly from a URL or link.
  2. Step 2: Identify the main purpose

    This helps users navigate directly to content without opening the app's home screen first.
  3. Final Answer:

    To open specific screens in the app from external links -> Option B
  4. Quick Check:

    Deep linking = open specific screens [OK]
Hint: Deep linking opens app screens from links [OK]
Common Mistakes:
  • Confusing deep linking with app performance
  • Thinking it handles animations
  • Assuming it manages authentication
2. Which React Native API is used to listen for incoming deep links?
easy
A. AsyncStorage
B. Navigation
C. Linking
D. Animated

Solution

  1. Step 1: Recall React Native APIs

    React Native provides the Linking API to handle URLs and deep links.
  2. Step 2: Match API to deep linking

    Linking listens for URL events and opens the app screen accordingly.
  3. Final Answer:

    Linking -> Option C
  4. Quick Check:

    Linking API = deep link listener [OK]
Hint: Use Linking API to handle URLs [OK]
Common Mistakes:
  • Choosing Navigation which manages screen transitions
  • Confusing with AsyncStorage for data storage
  • Picking Animated which is for animations
3. What will happen when this code runs and the app receives the URL myapp://profile/123?
Linking.addEventListener('url', (event) => {
  console.log(event.url);
});
medium
A. It logs 'myapp://profile/123' to the console
B. It throws a syntax error
C. It opens the profile screen automatically
D. It does nothing because the listener is incorrect

Solution

  1. Step 1: Understand the event listener

    The code listens for 'url' events and logs the event.url string.
  2. Step 2: Analyze the URL received

    When the app receives 'myapp://profile/123', the listener logs this exact string.
  3. Final Answer:

    It logs 'myapp://profile/123' to the console -> Option A
  4. Quick Check:

    Listener logs URL = correct output [OK]
Hint: Listener logs incoming URL string [OK]
Common Mistakes:
  • Assuming it opens screen automatically without navigation code
  • Thinking the code has syntax errors
  • Believing the listener does nothing
4. Identify the error in this deep linking setup code:
Linking.addEventListener('url', handleUrl);

function handleUrl(event) {
  console.log(event);
}

// Later
Linking.removeEventListener('url', handleUrl);
medium
A. removeEventListener should be called with a different event name
B. addEventListener requires a second argument as a string
C. handleUrl function should return a value
D. removeEventListener is deprecated; use remove() on the subscription

Solution

  1. Step 1: Check React Native Linking API usage

    In recent React Native versions, addEventListener returns a subscription object with a remove() method.
  2. Step 2: Identify correct removal method

    removeEventListener is deprecated; you should call subscription.remove() instead.
  3. Final Answer:

    removeEventListener is deprecated; use remove() on the subscription -> Option D
  4. Quick Check:

    Use subscription.remove() not removeEventListener [OK]
Hint: Use subscription.remove() to stop listening [OK]
Common Mistakes:
  • Calling removeEventListener which is deprecated
  • Expecting handleUrl to return a value
  • Passing wrong argument types to addEventListener
5. You want your React Native app to open the screen showing details for a product when a user clicks a link like myshop://product/42. Which steps are necessary to implement this deep linking behavior correctly?
hard
A. Configure URL scheme in app settings, listen for URL events, parse product ID, navigate to product screen
B. Only listen for URL events and log them; navigation happens automatically
C. Use AsyncStorage to save the URL, then read it on app start
D. Set up a web server to redirect links to the app

Solution

  1. Step 1: Configure app to handle custom URL scheme

    You must set up your app to recognize 'myshop://' links in platform config files.
  2. Step 2: Listen for URL events and parse the product ID

    Use Linking API to detect incoming URLs and extract the product ID from the URL path.
  3. Step 3: Navigate to the product screen with the parsed ID

    Use your navigation library to open the product detail screen with the correct ID.
  4. Final Answer:

    Configure URL scheme in app settings, listen for URL events, parse product ID, navigate to product screen -> Option A
  5. Quick Check:

    Full deep linking setup = Configure URL scheme in app settings, listen for URL events, parse product ID, navigate to product screen [OK]
Hint: Setup scheme, listen URL, parse, navigate [OK]
Common Mistakes:
  • Assuming navigation happens automatically without code
  • Using AsyncStorage instead of URL handling
  • Thinking a web server is needed for deep linking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard