Created
March 11, 2019 04:03
-
-
Save camiloibarrayepes/d9652217f7b7f7e5e5b3ffdf37687f8b to your computer and use it in GitHub Desktop.
Deep Link React Native Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import {Platform, View, Linking,} from 'react-native'; | |
| export default class deepLinkRN extends React.Component { | |
| constructor(props){ | |
| super(props) | |
| this.state = { } | |
| } | |
| componentDidMount() { | |
| //condition for the platform | |
| if (Platform.OS === 'android') { | |
| Linking.getInitialURL().then(url => { | |
| this.navigate(url); | |
| }); | |
| } else { | |
| Linking.addEventListener('url', this.handleOpenURL); | |
| } | |
| } | |
| componentWillUnmount() { | |
| Linking.removeEventListener('url', this.handleOpenURL); | |
| } | |
| handleOpenURL = (event) => { // D | |
| this.navigate(event.url); | |
| } | |
| navigate = (url) => { // E | |
| const { navigate } = this.props.navigation; | |
| const route = url.replace(/.*?:\/\//g, ''); | |
| //If you send a subitem like app://generic_option/0 you can use the following two lines | |
| //const id = route.match(/\/([^\/]+)\/?$/)[1]; | |
| //const routeName = route.split('/')[0]; | |
| //in the condition you may use routeName like this | |
| /*if (route === 'generic_option') { | |
| console.warn("is here" + id) | |
| };*/ | |
| if (route === 'generic_option') { | |
| console.warn("is here") | |
| }; | |
| } | |
| render() { | |
| return ( | |
| <View /> | |
| ); | |
| } | |
| } | |
| /*======== Instructions in IOS =========*/ | |
| //in the info.plist create a URL types | |
| //in the item 0 create URL schemes and in the intern item 0 set the value the link name for example "myapplink" | |
| //check the following link bit.ly/deepLinkRN | |
| /*======== Instructions in WEB =========*/ | |
| //just create a link with the name created in the infoplist | |
| //example: <a href="myapplink://generic_option>LINK</a> or <a href="myapplink://generic_option/0>LINK</a> if you created a subitem | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment