[RN] React Native 获取验证码 按钮
React Native 获取验证码 按钮
效果如图:


实现方法:
一、获取验证码 按钮组件 封装
CountDownButton.js
"use strict"; import React from 'react';
import PropTypes from 'prop-types'; import {
View,
Text,
TouchableOpacity,
ViewPropTypes, StyleSheet
} from 'react-native'; const defaultShowText = '获取验证码';
export default class CountDownButton extends React.Component {
constructor(props) {
super(props);
this.state = {
timerCount: this.props.timerCount || ,
timerTitle: this.props.timerTitle || defaultShowText,
counting: false,
selfEnable: true,
};
this._shouldStartCount = this._shouldStartCount.bind(this);
this._countDownAction = this._countDownAction.bind(this);
} static propTypes = {
style: ViewPropTypes.style,
textStyle: Text.propTypes.style,
onClick: PropTypes.func,
disableColor: PropTypes.string,
timerTitle: PropTypes.string,
enable: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
timerEnd: PropTypes.func,
timerActiveTitle: PropTypes.array,
executeFunc: PropTypes.func
}; _countDownAction() {
const codeTime = this.state.timerCount;
const {timerActiveTitle, timerTitle} = this.props;
const now = Date.now();
const overTimeStamp = now + codeTime * + ;
/*过期时间戳(毫秒) +100 毫秒容错*/
this.interval = setInterval(() => {
const nowStamp = Date.now();
if (nowStamp >= overTimeStamp) {
this.interval && clearInterval(this.interval);
this.setState({
timerCount: codeTime,
timerTitle: timerTitle || defaultShowText,
counting: false,
selfEnable: true
});
if (this.props.timerEnd) {
this.props.timerEnd()
}
} else {
const leftTime = parseInt((overTimeStamp - nowStamp) / , );
let activeTitle = `重新获取(${leftTime}s)`;
if (timerActiveTitle) {
if (timerActiveTitle.length > ) {
activeTitle = timerActiveTitle[] + leftTime + timerActiveTitle[]
} else if (timerActiveTitle.length > ) {
activeTitle = timerActiveTitle[] + leftTime
}
}
this.setState({
timerCount: leftTime,
timerTitle: activeTitle,
})
}
}, )
} _shouldStartCount(shouldStart) {
if (this.state.counting) {
return
}
if (shouldStart) {
this._countDownAction();
this.setState({counting: true, selfEnable: false})
} else {
this.setState({selfEnable: true})
}
} componentDidMount() {
const {executeFunc} = this.props;
executeFunc && executeFunc(this._shouldStartCount);
} componentWillUnmount() {
clearInterval(this.interval)
} render() {
const {onClick, style, textStyle, enable, disableColor} = this.props;
const {counting, timerTitle, selfEnable} = this.state;
return (
<View style={[{width: , height: }, style]}>
<TouchableOpacity
activeOpacity={counting ? : 0.8}
onPress={() => {
if (!counting && enable && selfEnable) { this.setState({selfEnable: false});
onClick(this._shouldStartCount)
}
}}
style={[styles.container,
{backgroundColor: ((!counting && enable && selfEnable) ? 'red' : disableColor || '#ccc')}
]}
>
<Text
style={[
styles.defaultText,
textStyle,
]}>{timerTitle}</Text>
</TouchableOpacity>
</View>
)
}
} const styles = StyleSheet.create({
container: {
flex: ,
justifyContent: 'center',
alignItems: 'center',
borderWidth: 0.5,
borderRadius: ,
borderColor: "white",
},
defaultText: {
fontSize: ,
color: "white",
}
});
使用:
import React, {Component} from "react";
import {StyleSheet, View,} from 'react-native';
import CountDownButton from './CountDownButton';
export default class TestButton extends Component {
constructor(props) {
super(props);
this.state = {}
}
render() {
return (
<View style={{flex: }}>
<CountDownButton enable={true}
timerCount={}
onClick={(_shouldStartCount) => {
_shouldStartCount(true)
}}/>
</View>
);
}
}
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/11031600.html
转载请著名出处!谢谢~~
[RN] React Native 获取验证码 按钮的更多相关文章
- [RN] React Native 获取地理位置
React Native 获取地理位置 实现原理: 1.用 navigator.geolocation.getCurrentPosition 获取到坐标信息 2.调用 高德地图 接口,解析位置数据 ...
- [RN] React Native 幻灯片效果 Banner
[RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...
- [RN] React Native 实现 类似QQ 登陆页面
[RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...
- iOS “获取验证码”按钮的倒计时功能
iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...
- [RN] React Native 实现图片预览
[RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...
- [RN] React Native 常见基本问题归纳总结
[RN] React Native 常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...
- [RN] React Native 关闭所有黄色警告
[RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...
- [RN] React Native 下实现底部标签(支持滑动切换)
上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...
- [RN] React Native 常用命令行
[RN] React Native 常用命令行 1.查看当前版本 react-native --version 或 react-native -v 2.创建指定版本的React Native项目 1) ...
随机推荐
- MySQL子查询结果集是否有记录
Mark SELECT tu.id userId, tu.avatar_url avatarUrl, tu.wx_nick_name wxNickName, tu.city city, (select ...
- windows环境下 mysql 忘记root密码时的解决办法
1,停止MYSQL服务,CMD打开DOS窗口,输入 net stop mysql 2,在CMD命令行窗口,进入MYSQL安装目录 比如E:\Program Files\MySQL\MySQL Serv ...
- TensorFlow的数据读取机制
一.tensorflow读取机制图解 首先需要思考的一个问题是,什么是数据读取?以图像数据为例,读取的过程可以用下图来表示 假设我们的硬盘中有一个图片数据集0001.jpg,0002.jpg,0003 ...
- 解密“CDO-首席数据官”的价值、挑战及发展
数据,不论形态.格式和类型,已经迅速成为企业最有战略意义的资产:数据资产已经成为了可以形成业务洞察及优势的战略资源,数据的体量.多样性和复杂性也正以指数级增长.就像其他重要的企业资产,数据需要适当的管 ...
- WPF 精修篇 WPF 使用ActiveX
原文:WPF 精修篇 WPF 使用ActiveX WPF 实现远程桌面功能 首先使用 开发人员命令提示 进入 自己的项目文件根目录下 输入 aximp C:\windows\System32\msts ...
- LearnOpenGL笔记(2)三角形
这是学习LearnOpenGL CN教程的笔记,包括我遇到的问题和我的烂笔头.文章名与网站小节对应. ------------------------------------分割线---------- ...
- 将canvas中左上角的原点坐标位置改为左下角
在使用canvas的时候,原点坐标在左上角,这个很犯人,因为一般的坐标基本都是在左下角,即笛卡尔坐标系,那怎么进行转变呢,在这里用到了canvas的translate,rotate,和scale进行转 ...
- vue 的 Class 与 Style 绑定
操作元素的 class 列表和内联样式是数据绑定的一个常见需求.因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼接麻烦且易错.因此,在将 ...
- Spring Boot2(八):性感banner,在线发牌
本文在个人技术博客[鸟不拉屎]同步发布,详情可猛戳 亦可扫描文章末尾二维码关注个人公众号[鸟不拉屎] emmm,没有啥前言 玩过SpringBoot的都知道,SpringBoot启动的时候,默认会在控 ...
- 使用tcpdump监控http流量
使用tcpdump监控http流量,具体内容包括:http request,http response,http headers以及http message body. 监控本机http流量 tcpd ...