React Native 获取验证码 按钮

效果如图:

实现方法:

一、获取验证码 按钮组件 封装

  1. CountDownButton.js
  1. "use strict";
  2.  
  3. import React from 'react';
  4. import PropTypes from 'prop-types';
  5.  
  6. import {
  7. View,
  8. Text,
  9. TouchableOpacity,
  10. ViewPropTypes, StyleSheet
  11. } from 'react-native';
  12.  
  13. const defaultShowText = '获取验证码';
  14. export default class CountDownButton extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. timerCount: this.props.timerCount || ,
  19. timerTitle: this.props.timerTitle || defaultShowText,
  20. counting: false,
  21. selfEnable: true,
  22. };
  23. this._shouldStartCount = this._shouldStartCount.bind(this);
  24. this._countDownAction = this._countDownAction.bind(this);
  25. }
  26.  
  27. static propTypes = {
  28. style: ViewPropTypes.style,
  29. textStyle: Text.propTypes.style,
  30. onClick: PropTypes.func,
  31. disableColor: PropTypes.string,
  32. timerTitle: PropTypes.string,
  33. enable: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
  34. timerEnd: PropTypes.func,
  35. timerActiveTitle: PropTypes.array,
  36. executeFunc: PropTypes.func
  37. };
  38.  
  39. _countDownAction() {
  40. const codeTime = this.state.timerCount;
  41. const {timerActiveTitle, timerTitle} = this.props;
  42. const now = Date.now();
  43. const overTimeStamp = now + codeTime * + ;
  44. /*过期时间戳(毫秒) +100 毫秒容错*/
  45. this.interval = setInterval(() => {
  46. const nowStamp = Date.now();
  47. if (nowStamp >= overTimeStamp) {
  48. this.interval && clearInterval(this.interval);
  49. this.setState({
  50. timerCount: codeTime,
  51. timerTitle: timerTitle || defaultShowText,
  52. counting: false,
  53. selfEnable: true
  54. });
  55. if (this.props.timerEnd) {
  56. this.props.timerEnd()
  57. }
  58. } else {
  59. const leftTime = parseInt((overTimeStamp - nowStamp) / , );
  60. let activeTitle = `重新获取(${leftTime}s)`;
  61. if (timerActiveTitle) {
  62. if (timerActiveTitle.length > ) {
  63. activeTitle = timerActiveTitle[] + leftTime + timerActiveTitle[]
  64. } else if (timerActiveTitle.length > ) {
  65. activeTitle = timerActiveTitle[] + leftTime
  66. }
  67. }
  68. this.setState({
  69. timerCount: leftTime,
  70. timerTitle: activeTitle,
  71. })
  72. }
  73. }, )
  74. }
  75.  
  76. _shouldStartCount(shouldStart) {
  77. if (this.state.counting) {
  78. return
  79. }
  80. if (shouldStart) {
  81. this._countDownAction();
  82. this.setState({counting: true, selfEnable: false})
  83. } else {
  84. this.setState({selfEnable: true})
  85. }
  86. }
  87.  
  88. componentDidMount() {
  89. const {executeFunc} = this.props;
  90. executeFunc && executeFunc(this._shouldStartCount);
  91. }
  92.  
  93. componentWillUnmount() {
  94. clearInterval(this.interval)
  95. }
  96.  
  97. render() {
  98. const {onClick, style, textStyle, enable, disableColor} = this.props;
  99. const {counting, timerTitle, selfEnable} = this.state;
  100. return (
  101. <View style={[{width: , height: }, style]}>
  102. <TouchableOpacity
  103. activeOpacity={counting ? : 0.8}
  104. onPress={() => {
  105. if (!counting && enable && selfEnable) {
  106.  
  107. this.setState({selfEnable: false});
  108. onClick(this._shouldStartCount)
  109. }
  110. }}
  111. style={[styles.container,
  112. {backgroundColor: ((!counting && enable && selfEnable) ? 'red' : disableColor || '#ccc')}
  113. ]}
  114. >
  115. <Text
  116. style={[
  117. styles.defaultText,
  118. textStyle,
  119. ]}>{timerTitle}</Text>
  120. </TouchableOpacity>
  121. </View>
  122. )
  123. }
  124. }
  125.  
  126. const styles = StyleSheet.create({
  127. container: {
  128. flex: ,
  129. justifyContent: 'center',
  130. alignItems: 'center',
  131. borderWidth: 0.5,
  132. borderRadius: ,
  133. borderColor: "white",
  134. },
  135. defaultText: {
  136. fontSize: ,
  137. color: "white",
  138. }
  139. });

使用:

  1. import React, {Component} from "react";
  2. import {StyleSheet, View,} from 'react-native';
  3. import CountDownButton from './CountDownButton';
  4.  
  5. export default class TestButton extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {}
  9. }
  10.  
  11. render() {
  12.  
  13. return (
  14. <View style={{flex: }}>
  15.  
  16. <CountDownButton enable={true}
  17. timerCount={}
  18. onClick={(_shouldStartCount) => {
  19. _shouldStartCount(true)
  20. }}/>
  21.  
  22. </View>
  23. );
  24.  
  25. }
  26. }

本博客地址: 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. Visual Studio2017专业版和企业版密钥

    Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF

  2. laravel5.5框架中视图间如何共享数据?视图间共享数据的两种方法

    laravel框架中视图间共享数据有两种,一种是用视图门面share()方法实现,另一种是用视图门面composer() 方法实现,那么,两种方法的实现究竟是怎样的呢?让我们来看一看接下来的文章内容. ...

  3. CountdownLatch例子

    CountdownLatch 一个线程或者多个线程等待其他线程完成了再接着往下执行 public class CountDownLatchTest { ); private static Random ...

  4. Rider中Winform开发支持预览(5)

    1.Rider .netCore3.0 winform设计器支持预览,这点vs目前还不支持. 2.不过winform下控件选择工具栏都是没有图标的

  5. scala练习题--万年历

     使用方法去完成 import scala.io.StdIn object work1 { def main(args: Array[String]): Unit = { // 1.先输出提示语句,并 ...

  6. Dart面向对象编程(二)

    继承: person.dart: class Person{ String name; int age; String _birthday; bool get isAdult => age &g ...

  7. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  8. 提高性能,MySQL 读写分离环境搭建(一)

    这是松哥之前一个零散的笔记,整理出来分享给大伙! MySQL 读写分离在互联网项目中应该算是一个非常常见的需求了.受困于 Linux 和 MySQL 版本问题,很多人经常会搭建失败,今天松哥就给大伙举 ...

  9. 02、策略模式(Strategy)

    一.概念: 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离, 手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法, 实现方法要根据具体的环境因素而变化. 二. ...

  10. torch.max

    torch.max() torch.max(input) -> Tensor Explation: ​ Returns the maximum value of all elements in ...