DEV Community

Cover image for How to Set Up and Use Deep Linking in React Native
Aneeqa Khan
Aneeqa Khan

Posted on

4

How to Set Up and Use Deep Linking in React Native

Introduction

Deep linking is a powerful technique in mobile app development that allows users to navigate directly to specific content within an app using a URL. In React Native, deep linking enables seamless user experiences by integrating the app with external sources such as emails, social media, and web pages.


Why Use Deep Linking?

  • Improved User Experience: Users can directly land on specific content without manually navigating through the app.
  • Marketing Campaigns: Helps in tracking user engagement from external sources.
  • Cross-App Navigation: Enables smooth transitions between different applications.

Types of Deep Linking

1. Traditional Deep Linking

  • Uses a predefined URL scheme (myapp://)
  • Requires the app to be installed
  • Example: myapp://profile/123

2. Universal Links (iOS) & App Links (Android)

  • Uses HTTP/HTTPS links (e.g., https://myapp.com/profile/123)
  • Opens the app if installed, otherwise, it redirects to a webpage
  • Requires domain verification

Setting Up Deep Linking in React Native

1. Configure Deep Linking in React Navigation

If you’re using React Navigation, you can set up deep linking as follows:

Install Required Dependencies:

npm install @react-navigation/native react-native-screens react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-vector-icons react-native-linking
Enter fullscreen mode Exit fullscreen mode

Define the Linking Configuration:

import { NavigationContainer } from '@react-navigation/native';

const linking = {
  prefixes: ['myapp://', 'https://myapp.com'],
  config: {
    screens: {
      Home: 'home',
      Profile: 'profile/:id',
    },
  },
};

export default function App() {
  return (
    <NavigationContainer linking={linking}>
      {/* App Stack */}
    </NavigationContainer>
  );
}
Enter fullscreen mode Exit fullscreen mode

2. Handling Incoming Links

To handle deep links, use React Native’s Linking API:

import { useEffect } from 'react';
import { Linking } from 'react-native';

const handleDeepLink = (event) => {
  console.log('Deep Link:', event.url);
};

useEffect(() => {
  Linking.addEventListener('url', handleDeepLink);
  return () => {
    Linking.removeEventListener('url', handleDeepLink);
  };
}, []);
Enter fullscreen mode Exit fullscreen mode

3. Configure iOS and Android

iOS (Info.plist)

Add the URL scheme in Info.plist:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>myapp</string>
    </array>
  </dict>
</array>
Enter fullscreen mode Exit fullscreen mode

For Universal Links, configure apple-app-site-association.

Android (AndroidManifest.xml)

Add intent filters in AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="myapp" android:host="profile" />
</intent-filter>
Enter fullscreen mode Exit fullscreen mode

For App Links, verify the domain using assetlinks.json.


Testing Deep Links

Use the following commands to test deep linking:

adb shell am start -W -a android.intent.action.VIEW -d "myapp://profile/123" com.myapp
Enter fullscreen mode Exit fullscreen mode

For iOS, use Safari or Xcode’s scheme tester.


Conclusion

Deep linking in React Native enhances user engagement by allowing direct navigation to specific app screens. By setting up deep linking correctly, you can improve user experience, streamline marketing efforts, and integrate seamlessly with external platforms.

Thank you for reading! Feel free to connect with me on LinkedIn or GitHub.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay