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 相关文章推荐
ExtJs扩展之GroupPropertyGrid代码
Mar 05 Javascript
IE6已终止操作问题的2种情况及解决
Apr 23 Javascript
jQuery匹配文档链接并添加class的方法
Jun 26 Javascript
JavaScript入门基础
Aug 12 Javascript
js采用concat和sort将N个数组拼接起来的方法
Jan 21 Javascript
Fullpage.js固定导航栏-实现定位导航栏
Mar 17 Javascript
jQuery实现点击弹出背景变暗遮罩效果实例代码
Jun 24 Javascript
JS获取IE版本号与HTML设置IE文档模式的方法
Oct 09 Javascript
微信小程序 textarea 组件详解及简单实例
Jan 10 Javascript
详解Vue.js搭建路由报错 router.map is not a function
Jun 27 Javascript
jQuery 选择器用法实例分析【prev + next】
May 22 jQuery
jQuery实现动态加载瀑布流
Sep 01 jQuery
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实现智能文件类型检测的实现代码
2011/08/02 PHP
php设置页面超时时间解决方法
2015/09/22 PHP
PHP精确到毫秒秒杀倒计时实例详解
2019/03/14 PHP
js 页面执行时间计算代码
2009/03/04 Javascript
Prototype源码浅析 Enumerable部分之each方法
2012/01/16 Javascript
js中top/parent/frame概述及案例应用
2013/02/06 Javascript
浅谈$(document)和$(window)的区别
2015/07/15 Javascript
jQuery插件datatables使用教程
2016/04/21 Javascript
js中class的点击事件没有效果的解决方法
2016/10/13 Javascript
jQuery实现根据生日计算年龄 星座 生肖
2016/11/23 Javascript
jquery实现输入框实时输入触发事件代码
2016/12/21 Javascript
ES6中module模块化开发实例浅析
2017/04/06 Javascript
Javascript(es2016) import和require用法和区别详解
2017/08/11 Javascript
浅谈es6语法 (Proxy和Reflect的对比)
2017/10/24 Javascript
动手写一个angular版本的Message组件的方法
2017/12/16 Javascript
vue.js绑定事件监听器示例【基于v-on事件绑定】
2018/07/07 Javascript
微信小程序如何再次获取用户授权的方法
2019/05/10 Javascript
搭建一个Koa后端项目脚手架的方法步骤
2019/05/30 Javascript
vue实现跳转接口push 转场动画示例
2019/11/01 Javascript
微信小程序开发摇一摇功能
2019/11/22 Javascript
javascript实现随机抽奖功能
2020/12/30 Javascript
[01:09:23]KG vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
[34:41]夜魇凡尔赛茶话会 第二期02:你画我猜
2021/03/11 DOTA
分享15个最受欢迎的Python开源框架
2014/07/13 Python
Python实现合并同一个文件夹下所有PDF文件的方法示例
2018/04/28 Python
基于数据归一化以及Python实现方式
2018/07/11 Python
python 实现逻辑回归
2020/12/30 Python
详解css3自定义滚动条样式写法
2017/12/25 HTML / CSS
倡议书格式模板
2014/05/13 职场文书
幼儿园六一儿童节活动方案
2014/08/26 职场文书
县委党的群众路线教育实践活动工作情况报告
2014/10/25 职场文书
夫妻分居协议书范本(有子女版)
2014/11/01 职场文书
redis内存空间效率问题的深入探究
2021/05/17 Redis
学会Python数据可视化必须尝试这7个库
2021/06/16 Python
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
Nginx代理Redis哨兵主从配置的实现
2022/07/15 Servers