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 获取验证码 按钮的更多相关文章

  1. [RN] React Native 获取地理位置

    React Native 获取地理位置 实现原理: 1.用  navigator.geolocation.getCurrentPosition 获取到坐标信息 2.调用 高德地图 接口,解析位置数据 ...

  2. [RN] React Native 幻灯片效果 Banner

    [RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...

  3. [RN] React Native 实现 类似QQ 登陆页面

    [RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...

  4. iOS “获取验证码”按钮的倒计时功能

    iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...

  5. [RN] React Native 实现图片预览

    [RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...

  6. [RN] React Native 常见基本问题归纳总结

    [RN] React Native  常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...

  7. [RN] React Native 关闭所有黄色警告

    [RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...

  8. [RN] React Native 下实现底部标签(支持滑动切换)

    上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...

  9. [RN] React Native 常用命令行

    [RN] React Native 常用命令行 1.查看当前版本 react-native --version 或 react-native -v 2.创建指定版本的React Native项目 1) ...

随机推荐

  1. hibernate中many-to-one的not-found属性和@notfound注解

    如果在Hibernate项目里面配了很多many-to-one的关联,即使后台在查询数据时已经作了健全性判断,但还是会经常抛出对象找不到异常:org.hibernate.ObjectNotFoundE ...

  2. WPF XAML Trigger中使用动画后 动画对象冻结的处理办法

    在编写XAML时 在Trigger中使用动画,在动画之后,动画对象就会被冻结,无法被其他动画或者属性改变. 处理办法有: 1 使用附加属性来添加动画 public static readonly De ...

  3. Prometheus 监控目标运行状态并邮件通知

    Prometheus 监控目标运行状态并邮件通知 邮件服务安装:https://www.cnblogs.com/xiangsikai/p/9809654.html 告警规则示例:https://pro ...

  4. 用itemloader提取,清洗数据的技巧

    def parse_item(self, response): item_loader = NewItemLoader(NewItem(), response) item_loader.add_xpa ...

  5. Kubernetes中的Volume介绍

    Kubernetes中支持的所有磁盘挂载卷简介发表于 2018年1月26日 Weihai Feb 10,2016 7400 字 | 阅读需要 15 分钟 容器磁盘上的文件的生命周期是短暂的,这就使得在 ...

  6. sedlauncher.exe 磁盘爆满

    打开应用和功能,搜KB4023057,然后卸载. 快捷键WIN+R打开运行,输入services.msc回车打开系统服务,找到Windows Remediation Service (sedsvc)和 ...

  7. Objective-C的self.用法

    转自:http://mobile.51cto.com/iphone-313096.htm 进入正题, 我们经常会在官方文档里看到这样的代码: MyClass.h @interface MyClass ...

  8. Burp Suite渗透实战操作指南-上篇

    Burp必备知识 在介绍功能之前有必要让大家了解一些burp的常用功能,以便在使用中更好的发挥麒麟臂的优势. 1.1  快捷键 很多人可能都没用过burp的快捷键吧,位置如下,不说话,如果不顺手可以自 ...

  9. uni-app通过判断接口403跳转登录页面的问题

    需求: 当向后端请求接口返回 403 的时候,直接跳到登录页面让用户登录 实现方案: 在请求结束后拦截器中判断,如果是 403 就跳转 遇到难题: 一级页面(我的) onShow() 中有接口请求,用 ...

  10. MySQL索引(九)

    一.索引介绍 1.1 什么是索引 索引就好比一本书的目录,它会让你更快的找到内容. 让获取的数据更有目的性,从而提高数据库检索数据的性能. 分为以下四种: BTREE:B+树索引(基本上都是使用此索引 ...