React Native使用Modal自定义分享界面的示例代码


Posted in Javascript onOctober 31, 2017

在很多App中都会涉及到分享,React Native提供了Modal组件用来实现一些模态弹窗,例如加载进度框,分享弹框等。使用Modal搭建分析的效果如下:

React Native使用Modal自定义分享界面的示例代码React Native使用Modal自定义分享界面的示例代码

自定义的分析界面代码如下:

ShareAlertDialog.js

/**
 * https://github.com/facebook/react-native
 * @flow 分享弹窗
 */


import React, {Component} from 'react';
import {View, TouchableOpacity, Alert,StyleSheet, Dimensions, Modal, Text, Image} from 'react-native';
import Separator from "./Separator";

const {width, height} = Dimensions.get('window');
const dialogH = 110;

export default class ShareAlertDialog extends Component {

  constructor(props) {
    super(props);
    this.state = {
      isVisible: this.props.show,
    };
  }

  componentWillReceiveProps(nextProps) {
    this.setState({isVisible: nextProps.show});
  }

  closeModal() {
    this.setState({
      isVisible: false
    });
    this.props.closeModal(false);
  }

  renderDialog() {
    return (
      <View style={styles.modalStyle}>
        <Text style={styles.text}>选择分享方式</Text>
        <Separator/>
        <View style={{flex: 1, flexDirection: 'row', marginTop: 15}}>
          <TouchableOpacity style={styles.item} onPress={() => Alert.alert('分享到微信朋友圈')}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_friends.png')}/>
            <Text>微信朋友圈</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.item}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_weixin.png')}/>
            <Text>微信好友</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.item}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_weibo.png')}/>
            <Text>新浪微博</Text>
          </TouchableOpacity>
        </View>
      </View>
    )
  }

  render() {

    return (
      <View style={{flex: 1}}>
        <Modal
          transparent={true}
          visible={this.state.isVisible}
          animationType={'fade'}
          onRequestClose={() => this.closeModal()}>
          <TouchableOpacity style={styles.container} activeOpacity={1}
                   onPress={() => this.closeModal()}>
            {this.renderDialog()}
          </TouchableOpacity>
        </Modal>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
  },
  modalStyle: {
    position: "absolute",
    top: height - 170,
    left: 0,
    width: width,
    height: dialogH,
    backgroundColor: '#ffffff'
  },
  subView: {
    width: width,
    height: dialogH,
    backgroundColor: '#ffffff'
  },
  text: {
    flex: 1,
    fontSize: 18,
    margin: 10,
    justifyContent: 'center',
    alignItems: 'center',
    alignSelf: 'center'
  },
  item: {
    width: width / 3,
    height: 100,
    alignItems: 'center',
    backgroundColor: '#ffffff'
  },
  image: {
    width: 60,
    height: 60,
    marginBottom: 8
  },
});

当点击某个按钮之后,就会弹出框,示例代码如下:

constructor(props) {
    super(props);
    this.state = {
      showSharePop: false,//分享弹窗,默认不显示
    }
  }


//省略

onSharePress() {
    this.setState({showSharePop: !this.state.showSharePop})
  }
//增加点击
<NavigationBar
          navigator={this.props.navigator}
          popEnabled={false}
          style={{backgroundColor: "transparent", position: "absolute", top: 0, width}}
          leftButton={ViewUtils.getLeftButton(() => this.props.navigator.pop())}
          rightButton={ViewUtils.getShareButton(() => this.onSharePress())}/>

//添加ShareAlertDialog自定义组件
<ShareAlertDialog show={this.state.showSharePop} closeModal={(show) => {
          this.setState({showSharePop: show})
        }} {...this.props}/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript 比较时间大小的代码
Apr 24 Javascript
JQuery文本改变触发事件如聚焦事件、失焦事件
Jan 15 Javascript
JS遍历Json字符串中键值对先转成JSON对象再遍历
Aug 15 Javascript
node.js中的fs.closeSync方法使用说明
Dec 17 Javascript
JS特效实现图片自动播放并可控的效果
Jul 31 Javascript
Bootstrap每天必学之标签页(Tab)插件
Aug 09 Javascript
想学习javascript JS和jQuery哪个重要 先学哪个
Dec 11 Javascript
JS验证全角与半角及相互转化的介绍
May 18 Javascript
详解JS取出两个数组中的不同或相同元素
Mar 20 Javascript
了解JavaScript表单操作和表单域
May 27 Javascript
JavaScript实现多个物体同时运动
Mar 12 Javascript
Vue CLI中模式与环境变量的深入详解
May 30 Vue.js
Bootstrap3.3.7导航栏下拉菜单鼠标滑过展开效果
Oct 31 #Javascript
使用nvm管理不同版本的node与npm的方法
Oct 31 #Javascript
javascript高级模块化require.js的具体使用方法
Oct 31 #Javascript
JS简单实现点击跳转登陆邮箱功能的方法
Oct 31 #Javascript
jQuery简单实现对数组去重及排序操作实例
Oct 31 #jQuery
Node.js学习教程之HTTP/2服务器推送【译】
Oct 31 #Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
Oct 31 #Javascript
You might like
PHP中iconv函数转码时截断字符问题的解决方法
2015/01/21 PHP
Mac环境下php操作mysql数据库的方法分享
2015/05/11 PHP
php post大量数据时发现数据丢失问题解决方法
2015/06/20 PHP
详解php比较操作符的安全问题
2015/12/03 PHP
详解PHP的Yii框架中自带的前端资源包的使用
2016/03/31 PHP
Mozilla 表达式 __noSuchMethod__
2009/04/05 Javascript
JQuery 风格的HTML文本转义
2009/07/01 Javascript
详解jQuery中的empty、remove和detach
2016/04/11 Javascript
jQuery树形插件jquery.simpleTree.js用法分析
2016/09/05 Javascript
Node.js利用debug模块打印出调试日志的方法
2017/04/25 Javascript
jQuery实现模糊查询的方法分析
2018/05/10 jQuery
ant design vue中表格指定格式渲染方式
2020/10/28 Javascript
[38:23]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS LGD第一场
2014/05/24 DOTA
[02:28]DOTA2 2015国际邀请赛中国区预选赛首日现场百态
2015/05/26 DOTA
[44:15]DOTA2上海特级锦标赛主赛事日 - 5 败者组决赛Liquid VS EG第二局
2016/03/06 DOTA
Python文件和目录操作详解
2015/02/08 Python
python3 pillow生成简单验证码图片的示例
2017/09/19 Python
Python基于PyGraphics包实现图片截取功能的方法
2017/12/21 Python
python基于ID3思想的决策树
2018/01/03 Python
Python中GIL的使用详解
2018/10/03 Python
pycharm配置git(图文教程)
2019/08/16 Python
Python 调用 Windows API COM 新法
2019/08/22 Python
Python3 翻转二叉树的实现
2019/09/30 Python
基于python实现地址和经纬度转换
2020/05/19 Python
美国著名珠宝品牌之一:Jared The Galleria Of Jewelry
2016/10/01 全球购物
英国国家美术馆商店:National Gallery
2019/05/01 全球购物
新驾驶员个人自我评价
2014/01/03 职场文书
烹调加工管理制度
2014/02/04 职场文书
三年级评语大全
2014/04/23 职场文书
学习经验演讲稿
2014/05/10 职场文书
幼儿教师小班个人总结
2015/02/05 职场文书
安娜卡列尼娜观后感
2015/06/11 职场文书
解决goland 导入项目后import里的包报红问题
2021/05/06 Golang
MySQL 自定义变量的概念及特点
2021/05/13 MySQL
排查Tomcat进程假死的问题
2022/05/06 Servers
springboot为异步任务规划自定义线程池的实现
2022/06/14 Java/Android