मैं एक प्रतिक्रियाशील एप्लिकेशन में हाइपरलिंक कैसे प्रदर्शित करूं?
जैसे
<a href="https://google.com>Google</a>
मैं एक प्रतिक्रियाशील एप्लिकेशन में हाइपरलिंक कैसे प्रदर्शित करूं?
जैसे
<a href="https://google.com>Google</a>
जवाबों:
कुछ इस तरह:
<Text style={{color: 'blue'}}
onPress={() => Linking.openURL('http://google.com')}>
Google
</Text>
उस Linking
मॉड्यूल का उपयोग करना जो प्रतिक्रियाशील मूल के साथ बंडल है।
this.props.url
स्थान पर कुछ का उपयोग कर सकते हैं 'http://google.com'
(कोई ब्रेस आवश्यक नहीं)
import { Linking } from 'react-native';
आपके दस्तावेज़ में है?
<Text>Some paragraph <Text onPress=...>with a link</Text> inside</Text>
चयनित उत्तर केवल आईओएस को संदर्भित करता है। दोनों प्लेटफार्मों के लिए, आप निम्नलिखित घटक का उपयोग कर सकते हैं:
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'
}
});
ऐसा करने के लिए, मैं दृढ़ता से एक Text
घटक को एक में लपेटने पर विचार करूंगा TouchableOpacity
। जब एक TouchableOpacity
को छुआ जाता है, तो वह फीका हो जाता है (कम अपारदर्शी हो जाता है)। यह पाठ को छूने पर उपयोगकर्ता को तत्काल प्रतिक्रिया देता है और एक बेहतर उपयोगकर्ता अनुभव प्रदान करता है।
लिंक बनाने के लिए आप onPress
संपत्ति का उपयोग कर सकते हैं TouchableOpacity
:
<TouchableOpacity onPress={() => Linking.openURL('http://google.com')}>
<Text style={{color: 'blue'}}>
Google
</Text>
</TouchableOpacity>
Linking
:import { Linking } from 'react-native';
const url="https://google.com"
<Text onPress={() => Linking.openURL(url)}>
{url}
</Text>
रिएक्टिव नेटिव हाइपरलिंक (नेटिव) का प्रयोग करें <A>
टैग) का :
इंस्टॉल:
npm i react-native-a
आयात:
import A from 'react-native-a'
उपयोग:
<A>Example.com</A>
<A href="example.com">Example</A>
<A href="https://example.com">Example</A>
<A href="example.com" style={{fontWeight: 'bold'}}>Example</A>
उपरोक्त प्रतिक्रियाओं को जोड़ने के लिए एक और उपयोगी नोट कुछ फ्लेक्सबॉक्स स्टाइल जोड़ना है। यह टेक्स्ट को एक लाइन पर रखेगा और सुनिश्चित करेगा कि टेक्स्ट स्क्रीन को ओवरलैप न करे।
<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>
रिएक्टिव नेटिव के लिए, ऐप में हाइपरलिंक्स खोलने के लिए लाइब्रेरी है। https://www.npmjs.com/package/react-native-hyperlink
इसके अलावा, मुझे लगता है कि आपको url की जांच करनी होगी और सबसे अच्छा तरीका है Regex। https://www.npmjs.com/package/url-regex
यदि आप लिंक और अन्य प्रकार के समृद्ध पाठ करना चाहते हैं, तो एक अधिक व्यापक समाधान रिएक्ट नेटिव HTMLView का उपयोग करना है ।
बस मैंने सोचा कि मैं किसी के साथ अपने हैक किए गए समाधान को साझा करूंगा, जो इस समस्या की खोज कर रहे हैं अब एक स्ट्रिंग के भीतर एम्बेडेड लिंक के साथ । यह लिंक को गतिशील रूप से रेंडर करके इनलाइन करने का प्रयास करता है , जिसमें कभी स्ट्रिंग को तंग किया जाता है।
कृपया इसे अपनी आवश्यकताओं के लिए बेझिझक महसूस करें। यह हमारे उद्देश्यों के लिए काम कर रहा है जैसे:
यह कैसे 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;
}
इंपोर्ट को रिएक्टिव नेटिव से मॉड्यूल लिंक करना
import { TouchableOpacity, Linking } from "react-native";
कोशिश करो:-
<TouchableOpacity onPress={() => Linking.openURL('http://Facebook.com')}>
<Text> Facebook </Text>
</TouchableOpacity>