Hyperlink means connect/Link a custom Text to a specific Website URL and File URL. It is very helpful in web development area but now we can show Add HyperLink Clickable Text in React Native both Android and iOS Applications using Linking library. So let’s get started .
Contents in this project Add HyperLink Clickable Text in React Native Android iOS React Native App :
1. Import StyleSheet, Text, View and Linking component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Linking } from ‘react-native’;
|
2. Create a Parent View in render’s return block.
1
2
3
4
5
6
7
8
9
10
|
render() {
return (
<View style={styles.MainContainer}>
</View>
);
}
|
3. Create a Text component in View. Now we would set onPress={} method on Text with Fat Array Function method and call the Linking.openURL(‘Put Your URL Here’) method. This would open the entered URL in mobile default web browser.
1
2
3
4
5
6
7
8
9
10
11
|
render() {
return (
<View style={styles.MainContainer}>
<Text style={styles.TextStyle} onPress={ ()=> Linking.openURL(‘https://google.com’) } >Click Here To Open Google.</Text>
</View>
);
}
|
4. Create Style for Parent View and Text component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’
},
TextStyle: {
color: ‘#E91E63’,
textDecorationLine: ‘underline’
}
});
|
5. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Linking } from ‘react-native’;
export default class App extends Component<{}> {
render() {
return (
<View style={styles.MainContainer}>
<Text style={styles.TextStyle} onPress={ ()=> Linking.openURL(‘https://google.com’) } >Click Here To Open Google.</Text>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’
},
TextStyle: {
color: ‘#E91E63’,
textDecorationLine: ‘underline’
}
});
|
Screenshot in Android device :