React-native deep linking with handle response from other app

251 views Asked by At

I have an expo app that uses expo-linking, it works great and now for some reason I want to switch to regular react-native.

I did a lot of searching online but still no success. Looking forward to a detailed answer. Thanks a lot!

Here is my code using expo-linking

import React, { useCallback, useEffect, useState } from 'react'
import { Button, View } from 'react-native'
import * as Linking from 'expo-linking'

const onConnectRedirectLink = Linking.createURL('onConnect')

export default function App() {
    const [deepLink, setDeepLink] = useState('')

    useEffect(() => {
        ; (async () => {
            const initialUrl = await Linking.getInitialURL()
            if (initialUrl) {
                setDeepLink(initialUrl)
            }
        })()
        Linking.addEventListener('url', handleDeepLink)
        return () => {
            Linking.removeEventListener('url', handleDeepLink)
        }
    }, [])

    const handleDeepLink = ({ url }) => {
        setDeepLink(url)
    }

    // handle inbounds links
    useEffect(() => {
        if (!deepLink) return

        const url = new URL(deepLink)

        if (/onConnect/.test(url.pathname)) {
            // handle response
        }
    }, [deepLink])

    const connect = async () => {
        // ...
        Linking.openURL(url)
    }

    return (
        <View style={{ flex: 1, backgroundColor: '#333' }}>
            <View style={{ flex: 0, paddingTop: 20, paddingBottom: 40 }}>
                <Button title='Connect' onPress={connect} />
            </View>
        </View>
    )
}
0

There are 0 answers