回答
事情是这样的:使用Linking
模块与本机作出反应是捆绑的
<Text style={{color: 'blue'}}
onPress={() => Linking.openURL('http://google.com')}>
Google
</Text>
。 (请注意,使用Linking
需要React Native 0.20.0或更高版本。)
这对我有效......谢谢! –
如果你需要一个动态的值,你可以使用'this.props.url'来替代''http:// google.com''(不需要大括号) –
@philipp它会抛出错误m'can' t找到变量链接' – devanshsadhotra
所选答案仅适用于iOS。对于这两个平台上,可以使用以下组件:
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'
}
});
如果你想要做的链接和其他类型的富文本的,更全面的解决方案是使用React Native HTMLView。
尽管这个链接可能回答这个问题,最好在这里包括答案的基本部分,提供链接供参考。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/13192218) – Ari0nhh
@ Ari0nhh我放弃了这个问题,因为这是我能回应您的评论的唯一方式。在SO上有很多先例,排名靠前的答案只是链接到一个好的图书馆。加上其他答案已经涵盖了一个简单的实现我想我可以重新发布这个作为对原始问题的评论,但我确实认为它是一个真正的答案。如果人们想要编辑它并通过更好的例子来改进它,至少现在有一个地方可以开始,那么把链接留在这里对于未来的搜索者来说至少是一个缺点。 – Eliot
对于React Native,有库可以在应用程序中打开超链接。 https://www.npmjs.com/package/react-native-hyperlink
除此之外,我想你会需要检查网址和最好的方法是正则表达式。 https://www.npmjs.com/package/url-regex
如果您正在为ios构建,SVC更好地实现而不是链接(在Safari浏览器中打开)。 https://github.com/naoufal/react-native-safari-view – rajaishwary
为此,我会强烈考虑将Text
组件包装在TouchableOpacity
中。当碰到TouchableOpacity
时,它会消失(变得不透明)。这在触摸文本时给予用户即时反馈并提供改进的用户体验。
您可以使用在TouchableOpacity
的onPress
属性,使链接发生:
<TouchableOpacity onPress={() => Linking.openURL('http://google.com')}>
<Text style={{color: 'blue'}}>
Google
</Text>
</TouchableOpacity>
- 1. 在React Native中进行链接
- 2. 在React Native中链接地图
- 3. 在React Native中链接CSS框架
- 4. React Native - No app-release.apk
- 5. React Native App - Google SignIn
- 6. React Native In App Utils
- 7. asp:超链接显示无超链接
- 8. React-Native:在ListView中显示JSON数据
- 9. 图像不显示在Lighbox React Native中
- 10. 如何在React Native中显示错误?
- 11. 循环Json&显示在React Native中
- 12. 模拟显示:在React Native中内联
- 13. 按钮不显示React-Native
- 14. 在iphone中显示超链接
- 15. Dlookup超链接在MsgBox中显示
- 16. 如何在jqGrid中显示超链接
- 17. 在php中显示超链接
- 18. 在javascript中显示超链接
- 19. 在createhtml中显示超链接
- 20. 在HTML.Raw中显示超链接
- 21. jqGrd超链接或显示链接
- 22. Warning with create-react-native-app
- 23. react-native app lag on android
- 24. React-Native App Structure - registerComponent('F8v2',setup);
- 25. Android - 超链接显示
- 26. WebStorm AngularJS,Angular CLI,React Native和React App
- 27. 超链接不显示
- 28. react-native 0.46链接命令在提示后退出?
- 29. Modal没有显示在react-native?
- 30. react-native - 如何使用react-native-maps显示地图
考虑增加更多的标签,如“JavaScript的”从用户那里得到更多的关注。但是不要通过添加“编码”等标签来过度概括你的帖子。 –