रिएक्टिव नेटिव ऐप में कोई हाइपरलिंक कैसे प्रदर्शित करता है?


110

मैं एक प्रतिक्रियाशील एप्लिकेशन में हाइपरलिंक कैसे प्रदर्शित करूं?

जैसे

<a href="https://google.com>Google</a> 

2
उपयोगकर्ताओं से अधिक ध्यान आकर्षित करने के लिए 'जावास्क्रिप्ट' जैसे और टैग जोड़ने पर विचार करें। लेकिन 'कोडिंग', इत्यादि जैसे टैग जोड़कर अपनी पोस्ट को सामान्य न करें
मैट सी

@MattC मैं तर्क दूंगा कि 'जावास्क्रिप्ट' को जोड़ना बहुत सामान्य है।
रैनबेजाकसन

जवाबों:


233

कुछ इस तरह:

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

उस Linkingमॉड्यूल का उपयोग करना जो प्रतिक्रियाशील मूल के साथ बंडल है।


1
यदि आपको एक गतिशील मूल्य की आवश्यकता है, तो आप इसके this.props.urlस्थान पर कुछ का उपयोग कर सकते हैं 'http://google.com'(कोई ब्रेस आवश्यक नहीं)
एलोन जीतो

@philipp यह मुझे त्रुटि फेंक रहा है 'चर नहीं मिल रहा है'
@ एफिलिप देवांश साधु

2
@ महादेवसुधोत्र import { Linking } from 'react-native';आपके दस्तावेज़ में है?
एरिक फिलिप्स

2
आप <पाठ> तत्वों को भी एम्बेड कर सकते हैं ताकि लिंक किए गए पाठ मूल पाठ का एक हिस्सा बन <Text>Some paragraph <Text onPress=...>with a link</Text> inside</Text>
सकें

4
लिंकिंगआईओएस को ह्रास किया गया है, लिंकिंग का उपयोग करें।
जस्सोनलहार्ड

26

चयनित उत्तर केवल आईओएस को संदर्भित करता है। दोनों प्लेटफार्मों के लिए, आप निम्नलिखित घटक का उपयोग कर सकते हैं:

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'
  }
});

3
चयनित उत्तर ने मेरे लिए Android (RN 35) में ठीक काम किया।
पेद्राम

2
@JacobLauritzen अच्छी तरह से अब यह वही है जिसके बाद किसी ने मेरे जवाब की नकल की: / stackoverflow.com/posts/30540502/revisions
Kuf

21

ऐसा करने के लिए, मैं दृढ़ता से एक Textघटक को एक में लपेटने पर विचार करूंगा TouchableOpacity। जब एक TouchableOpacityको छुआ जाता है, तो वह फीका हो जाता है (कम अपारदर्शी हो जाता है)। यह पाठ को छूने पर उपयोगकर्ता को तत्काल प्रतिक्रिया देता है और एक बेहतर उपयोगकर्ता अनुभव प्रदान करता है।

लिंक बनाने के लिए आप onPressसंपत्ति का उपयोग कर सकते हैं TouchableOpacity:

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

13

प्रतिक्रियाशील मूल दस्तावेज का उपयोग करने का सुझाव देता है Linking :

संदर्भ

यहाँ एक बहुत ही बुनियादी उपयोग मामला है:

import { Linking } from 'react-native';

const url="https://google.com"

<Text onPress={() => Linking.openURL(url)}>
    {url}
</Text>

आप कार्यात्मक या वर्ग घटक संकेतन, डीलरों की पसंद का उपयोग कर सकते हैं।


लिंकिंगआईओएस को ह्रास किया गया है, लिंकिंग का उपयोग करें।
जस्सोनलहार्ड

4

रिएक्टिव नेटिव हाइपरलिंक (नेटिव) का प्रयोग करें <A> टैग) का :

इंस्टॉल:

npm i react-native-a

आयात:

import A from 'react-native-a'

उपयोग:

  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>

3

उपरोक्त प्रतिक्रियाओं को जोड़ने के लिए एक और उपयोगी नोट कुछ फ्लेक्सबॉक्स स्टाइल जोड़ना है। यह टेक्स्ट को एक लाइन पर रखेगा और सुनिश्चित करेगा कि टेक्स्ट स्क्रीन को ओवरलैप न करे।

 <View style={{ display: "flex", flexDirection: "row", flex: 1, flexWrap: 'wrap', margin: 10 }}>
  <Text>Add your </Text>
  <TouchableOpacity>
    <Text style={{ color: 'blue' }} onpress={() => Linking.openURL('https://www.google.com')} >
         link
    </Text>
   </TouchableOpacity>
   <Text>here.
   </Text>
 </View>

2

रिएक्टिव नेटिव के लिए, ऐप में हाइपरलिंक्स खोलने के लिए लाइब्रेरी है। https://www.npmjs.com/package/react-native-hyperlink

इसके अलावा, मुझे लगता है कि आपको url की जांच करनी होगी और सबसे अच्छा तरीका है Regex। https://www.npmjs.com/package/url-regex


यदि आप आईओएस के लिए निर्माण कर रहे हैं, तो एसवीसी लिंकिंग (सफारी ब्राउज़र में खोलने के लिए) के बजाय कार्यान्वयन के लिए बेहतर दृष्टिकोण है। github.com/naoufal/react-native-safari-view
razishwary

1

यदि आप लिंक और अन्य प्रकार के समृद्ध पाठ करना चाहते हैं, तो एक अधिक व्यापक समाधान रिएक्ट नेटिव HTMLView का उपयोग करना है


1
हालांकि यह लिंक प्रश्न का उत्तर दे सकता है, लेकिन उत्तर के आवश्यक हिस्सों को यहां शामिल करना और संदर्भ के लिए लिंक प्रदान करना बेहतर है। लिंक-केवल उत्तर अमान्य हो सकते हैं यदि लिंक किए गए पृष्ठ बदल जाते हैं। - समीक्षा से
Ari0nhh

@ Ari0nhh मैंने सवाल को अनवील किया क्योंकि यह एकमात्र तरीका था जिससे मैं आपकी टिप्पणी का जवाब दे सकता था। SO पर बहुत सारी मिसालें हैं जहाँ एक उच्च रैंक वाला उत्तर केवल एक अच्छी लाइब्रेरी का लिंक है। साथ ही अन्य उत्तर पहले से ही एक साधारण कार्यान्वयन को कवर करते हैं। मुझे लगता है कि मैं इसे मूल प्रश्न पर टिप्पणी के रूप में दोहरा सकता हूं, लेकिन मैं इसे एक वास्तविक उत्तर के रूप में देखता हूं। और यहाँ लिंक को छोड़ना कम से कम भविष्य के चाहने वालों के लिए एक मुश्किल है, अगर लोग इसे संपादित करना चाहते हैं और इसे बेहतर उदाहरणों के साथ सुधारते हैं तो कम से कम अब शुरू होने के लिए एक जगह है।
एलियट

1

बस मैंने सोचा कि मैं किसी के साथ अपने हैक किए गए समाधान को साझा करूंगा, जो इस समस्या की खोज कर रहे हैं अब एक स्ट्रिंग के भीतर एम्बेडेड लिंक के साथ । यह लिंक को गतिशील रूप से रेंडर करके इनलाइन करने का प्रयास करता है , जिसमें कभी स्ट्रिंग को तंग किया जाता है।

कृपया इसे अपनी आवश्यकताओं के लिए बेझिझक महसूस करें। यह हमारे उद्देश्यों के लिए काम कर रहा है जैसे:

यह कैसे https://google.com का एक उदाहरण है दिखाई देगा।

इसे जिस्ट पर देखें:

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;
}

1

इंपोर्ट को रिएक्टिव नेटिव से मॉड्यूल लिंक करना

import { TouchableOpacity, Linking } from "react-native";

कोशिश करो:-

<TouchableOpacity onPress={() => Linking.openURL('http://Facebook.com')}>
     <Text> Facebook </Text>     
</TouchableOpacity>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.