Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one Display a Hyperlink in React Native App?

How do I display a hyperlink in a React Native app?

e.g.

<a href="https://google.com>Google</a> 
like image 830
Will Chu Avatar asked Sep 29 '22 22:09

Will Chu


People also ask

How do I open a link in React Native?

To open a URL in default web browser with React Native, we can use the Linking. openURL method.


2 Answers

Something like this:

<Text style={{color: 'blue'}}
      onPress={() => Linking.openURL('http://google.com')}>
  Google
</Text>

using the Linking module that's bundled with React Native.

import { Linking } from 'react-native';
like image 378
Philipp von Weitershausen Avatar answered Oct 10 '22 05:10

Philipp von Weitershausen


The selected answer refers only to iOS. For both platforms, you can use the following component:

import React, { Component, PropTypes } from 'react';
import {
  Linking,
  Text,
  StyleSheet
} from 'react-native';

export default class HyperLink extends Component {

  constructor(){
      super();
      this._goToURL = this._goToURL.bind(this);
  }

  static propTypes = {
    url: PropTypes.string.isRequired,
    title: PropTypes.string.isRequired,
  }

  render() {

    const { title} = this.props;

    return(
      <Text style={styles.title} onPress={this._goToURL}>
        >  {title}
      </Text>
    );
  }

  _goToURL() {
    const { url } = this.props;
    Linking.canOpenURL(url).then(supported => {
      if (supported) {
        Linking.openURL(this.props.url);
      } else {
        console.log('Don\'t know how to open URI: ' + this.props.url);
      }
    });
  }
}

const styles = StyleSheet.create({
  title: {
    color: '#acacac',
    fontWeight: 'bold'
  }
});
like image 33
Kuf Avatar answered Oct 10 '22 05:10

Kuf