ReactNative TextView 中的链接

And*_*rut 1 hyperlink react-native

我想在 ReactNative TextView 中添加一个外部链接。如何做到这一点?

我刚刚试过这个,它正在工作,但我希望所有链接都使用默认颜色。

<Text style={{color: 'blue'}}
      onPress={() => Linking.openURL('http://google.com')}>
  View more details
</Text>
Run Code Online (Sandbox Code Playgroud)

是否有仅用于链接生成的库 ReactNative 库?

小智 6

嗨,我会制作一个自定义组件,如下所示。

import * as React from 'react'
import {Text} from 'react-native'

export default ({url, text}) => {
  return <Text 
    style={{color: 'blue'}} 
    onPress={() => Linking.openUrl(url)}
  >{text}</Text>
}
Run Code Online (Sandbox Code Playgroud)

然后您可以按如下方式使用它。

import Link from './link' // Where ever it is.
import * as React from 'react'

export default() => {
  return <Link url="http://google.com" text="View more details" />
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以将所有链接设为蓝色,将来如果您更喜欢黄色,您可以更改一行,所有链接都会更改颜色。祝你好运,如果有什么不清楚的请告诉我。