Search code examples
javascriptreact-nativeweburltext

How to embed a redirection url to a website? (in React Native)


I would like to integrate this text in my app "https://www.google.com". When we click on it I will want the mobile browser to open and display the website. How to do this? I am novice, any help would be appreciate.

<TouchableOpacity>
<View>
    <Text style={{color: 'grey', fontSize: 17, fontFamily:'Arial', fontStyle: 'bold', textAlign: 'center', 
    marginTop: 6, marginLeft: 25, marginBottom: 20}}> 
                https://www.google.com/
    </Text>
  </View>
</TouchableOpacity>


Solution

  • You do not need to wrap Text inside a View and then inside TouchableOpacity, for a Text itself has onPress property. Avoid unnecessary nesting of elements.

    <Text style={{color: 'grey',
     fontSize: 17,
     fontFamily:'Arial',
     fontStyle: 'bold',
     textAlign: 'center', 
            marginTop: 6,
     marginLeft: 25,
     marginBottom: 20}}
     onPress = {() => {Linking.openURL('https://google.com')}}> 
      https://www.google.com/ /* Whatever appropriate text you want to display */
      </Text>