Understanding: React Native Linking

React Native linking is a powerful tool that allows you to link your React Native app to other apps and services.

Nandhu_writes
3 min readOct 17, 2023

Offer a coffee ☕️ before a clap👏🏻

What is linking in React Native?

Simply it can perform a variety of tasks, including:

  • Opening other apps from your React Native app
  • Handling deep links in your React Native app
  • Sharing content from y;:our React Native app to other apps
  • Receiving data from other apps in your React Native app

Two ways to handle URLs:

If the app is already open, the app is foregrounded and a Linking ‘URL’ event is fired​
You can handle these events with Linking.addEventListener('URL, callback) - it calls callback({url}) with the linked URL

If the app is not already open, it is opened and the URL is passed in as the initial URL

You can handle these events with Linking.getInitialURL() - it returns a Promise that resolves to the URL if there is one.

Here are some examples of advanced usage of React Native linking:

  • Using Universal Links or Android App Links to open your React Native app from a website or email link. This can be used to improve the user experience by allowing users to open your app directly from a link, without having to go to the App Store or Google Play.
  • Using deep links to navigate to specific screens in your React Native app. This can be used to provide users with a more direct way to access the content they’re looking for. For example, you could use a deep link to open a specific product page in your e-commerce app.
  • Using custom linking schemes to share content from your React Native app to other apps. This can be used to make it easier for users to share content from your app with their friends and family. For example, you could use a custom linking scheme to share a YouTube video from your app to the YouTube app.
  • Using custom linking schemes to receive data from other apps in your React Native app. This can be used to integrate your React Native app with other apps and services. For example, you could use a custom linking scheme to receive a user’s profile information from a social media app.

Here is an example of how to use React Native linking to open a specific screen in your React Native app from a deep link:

import React, { useState } from 'react';
import { Linking } from 'react-native';
const App = () => {
const [screen, setScreen] = useState('');
Linking.addEventListener('url', ({ url }) => {
const path = new URL(url).pathname;
setScreen(path.replace(/^\//, ''));
});
switch (screen) {
case 'product':
return <ProductScreen />;
default:
return <HomeScreen />;
}
};
export default App;

Here, we use the Linking.addEventListener() method to listen for deep links. When a deep link is received, we extract the path from the URL and use it to determine which screen to render.

You can use a similar approach to use React Native linking for other advanced tasks, such as sharing content to other apps or receiving data from other apps.

If you need help with using React Native linking for advanced usage, there are a number of resources available online, including the React Native documentation and community forums.

--

--

Nandhu_writes

React Native developer | Still a learner | blogger : Tech life lessons || Buy me a coffee, and I'll write : https://www.buymeacoffee.com/infoappmakk