How does one Display a Hyperlink in React Native App?

前端 未结 11 1190
孤街浪徒
孤街浪徒 2020-12-12 16:38

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

e.g.

Google 
相关标签:
11条回答
  • 2020-12-12 17:14

    React Native documentation suggests using Linking:

    Reference

    Here is a very basic use case:

    import { Linking } from 'react-native';
    
    const url="https://google.com"
    
    <Text onPress={() => Linking.openURL(url)}>
        {url}
    </Text>
    

    You can use either functional or class component notation, dealers choice.

    0 讨论(0)
  • 2020-12-12 17:17

    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'
      }
    });
    
    0 讨论(0)
  • 2020-12-12 17:17

    Just thought I'd share my hacky solution with anyone who's discovering this problem now with embedded links within a string. It attempts to inline the links by rendering it dynamically with what ever string is fed into it.

    Please feel free to tweak it to your needs. It's working for our purposes as such:

    This is an example of how https://google.com would appear.

    View it on Gist:

    https://gist.github.com/Friendly-Robot/b4fa8501238b1118caaa908b08eb49e2

    import React from 'react';
    import { Linking, Text } from 'react-native';
    
    export default function renderHyperlinkedText(string, baseStyles = {}, linkStyles = {}, openLink) {
      if (typeof string !== 'string') return null;
      const httpRegex = /http/g;
      const wwwRegex = /www/g;
      const comRegex = /.com/g;
      const httpType = httpRegex.test(string);
      const wwwType = wwwRegex.test(string);
      const comIndices = getMatchedIndices(comRegex, string);
      if ((httpType || wwwType) && comIndices.length) {
        // Reset these regex indices because `comRegex` throws it off at its completion. 
        httpRegex.lastIndex = 0;
        wwwRegex.lastIndex = 0;
        const httpIndices = httpType ? 
          getMatchedIndices(httpRegex, string) : getMatchedIndices(wwwRegex, string);
        if (httpIndices.length === comIndices.length) {
          const result = [];
          let noLinkString = string.substring(0, httpIndices[0] || string.length);
          result.push(<Text key={noLinkString} style={baseStyles}>{ noLinkString }</Text>);
          for (let i = 0; i < httpIndices.length; i += 1) {
            const linkString = string.substring(httpIndices[i], comIndices[i] + 4);
            result.push(
              <Text
                key={linkString}
                style={[baseStyles, linkStyles]}
                onPress={openLink ? () => openLink(linkString) : () => Linking.openURL(linkString)}
              >
                { linkString }
              </Text>
            );
            noLinkString = string.substring(comIndices[i] + 4, httpIndices[i + 1] || string.length);
            if (noLinkString) {
              result.push(
                <Text key={noLinkString} style={baseStyles}>
                  { noLinkString }
                </Text>
              );
            }
          }
          // Make sure the parent `<View>` container has a style of `flexWrap: 'wrap'`
          return result;
        }
      }
      return <Text style={baseStyles}>{ string }</Text>;
    }
    
    function getMatchedIndices(regex, text) {
      const result = [];
      let match;
      do {
        match = regex.exec(text);
        if (match) result.push(match.index);
      } while (match);
      return result;
    }
    
    0 讨论(0)
  • 2020-12-12 17:18

    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.

    0 讨论(0)
  • 2020-12-12 17:19

    for the React Native, there is library to open Hyperlinks in App. https://www.npmjs.com/package/react-native-hyperlink

    In addition to this, i suppose you will need to check url and best approach is Regex. https://www.npmjs.com/package/url-regex

    0 讨论(0)
  • 2020-12-12 17:22

    Use React Native Hyperlink (Native <A> tag):

    Install:

    npm i react-native-a
    

    import:

    import A from 'react-native-a'
    

    Usage:

    1. <A>Example.com</A>
    2. <A href="example.com">Example</A>
    3. <A href="https://example.com">Example</A>
    4. <A href="example.com" style={{fontWeight: 'bold'}}>Example</A>
    0 讨论(0)
提交回复
热议问题