To get parameters from a dynamic link in a React Native app, you can use the Firebase Dynamic Links library. Firebase Dynamic Links allow you to create custom URLs that direct users to specific content in your app. When a user clicks on the dynamic link, your app can retrieve the parameters associated with the link and use them to display the relevant content.

Here's how you can get parameters from a dynamic link in a React Native app using the Firebase Dynamic Links library:

  1. Install Dependencies: First, you need to install the required dependencies. Make sure you have already set up Firebase in your React Native project. If not, you can follow the Firebase setup guide for React Native.

    bash
    npm install @react-native-firebase/dynamic-links
  2. Set Up Firebase Dynamic Links: Follow the instructions in the Firebase documentation to set up Firebase Dynamic Links for your project. Make sure you have created dynamic links with custom parameters.

  3. Handle Dynamic Link in React Native: Now, you can handle dynamic links in your React Native code to retrieve the parameters. The following steps assume you have a main entry point where you handle dynamic links, such as the App.js file.

    jsx
    // App.js import React, { useEffect } from 'react'; import { Linking } from 'react-native'; import dynamicLinks from '@react-native-firebase/dynamic-links'; const App = () => { useEffect(() => { // Listen for incoming dynamic links when the app is opened const handleDynamicLink = async () => { const initialLink = await dynamicLinks().getInitialLink(); if (initialLink) { handleLink(initialLink.url); } }; handleDynamicLink(); // Listen for incoming dynamic links while the app is running const unsubscribe = dynamicLinks().onLink(handleLink); return () => unsubscribe(); }, []); const handleLink = (url) => { // Parse the URL and extract parameters const params = getParametersFromUrl(url); // Use the parameters in your app console.log('Dynamic Link Parameters:', params); }; const getParametersFromUrl = (url) => { // You need to extract and parse the parameters from the URL here // The URL might look like: https://yourdomain.com/?param1=value1&param2=value2 // You can use libraries like 'query-string' or custom parsing logic to extract the parameters. // For example, if using 'query-string': // const parsed = queryString.parse(url.split('?')[1]); // return parsed; // In this example, we assume you have a simple URL like: https://yourdomain.com/:param1/:param2 const splitUrl = url.split('/'); const param1 = splitUrl[3]; const param2 = splitUrl[4]; return { param1, param2 }; }; // ... rest of your app code }; export default App;

    The handleLink function is responsible for extracting the parameters from the dynamic link URL. The example assumes that the dynamic link is in the format https://yourdomain.com/:param1/:param2, where param1 and param2 are the custom parameters you want to retrieve. You might need to modify the getParametersFromUrl function to suit your specific dynamic link format.

That's it! Now, when users click on a dynamic link that leads to your app, the parameters will be extracted and logged in the console. You can modify the handleLink function to use the parameters in your app as needed.

Have questions or queries?
Get in Touch