按React Native Elements UI Toolkit中的按钮转到Web URL

时间:2017-12-06 00:49:39

标签: javascript reactjs button react-native react-native-android

我是新手,我尝试使用

制作社交媒体按钮
import { SocialIcon } from 'react-native-elements';

并使用

<SocialIcon type='facebook'/>

但如何在OnPress( )中发送网址。 ?

1 个答案:

答案 0 :(得分:1)

通过Linking in React Native

  

开始链接的相应活动(网址,电子邮件,   联系等),致电:

Linking.openURL(url).catch(err => console.error('An error occurred', err));

您可以将url换成真实网址,将其包含在传递到SocialIcon组件的onPress道具中的非常具体的处理程序中,或者(这很多)更多React-y),您可以创建一个可重用的组件,即:

class SocialButton extends Component {
  static propTypes = {
    url: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
  };

  handleClick = () => {
    Linking.openURL(this.props.url).catch(err => console.error("An error occurred", err));
  };

  render() {
    return <SocialIcon type={this.props.type} url={this.props.url} onPress={this.handleClick} />;
  }
}

并按以下方式使用:

<SocialButton type="facebook" url="https://www.facebook.com/wonderloveapp/" />