最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ReactNative短信驗(yàn)證碼倒計(jì)時(shí)控件的實(shí)現(xiàn)代碼

 更新時(shí)間:2017年07月20日 08:58:38   作者:關(guān)瑋琳linSir  
本篇文章主要介紹了ReactNative短信驗(yàn)證碼倒計(jì)時(shí)控件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

由于最近剛開始認(rèn)真的搞RN,可能有一些封裝的不是最佳實(shí)踐,還是希望大家多提意見,和大家一起進(jìn)步吧。本文介紹了ReactNative短信驗(yàn)證碼倒計(jì)時(shí)控件,分享給大家

功能

根據(jù)項(xiàng)目的需要,需要寫一個(gè)自定義的控件,實(shí)現(xiàn)如下功能:

  1. 默認(rèn)文字為點(diǎn)擊獲取驗(yàn)證碼
  2. 點(diǎn)擊后出現(xiàn)60秒的倒計(jì)時(shí)
  3. 顏色,字號可調(diào)
  4. 倒計(jì)時(shí)過程中,再次點(diǎn)擊需要忽略掉
  5. 倒計(jì)時(shí)完成后文本恢復(fù)成點(diǎn)擊獲取驗(yàn)證碼
  6. 再幾次點(diǎn)擊同之前

其實(shí)說了這么多,就是個(gè)最普通的驗(yàn)證碼的功能。。。

效果

效果圖如下:(錄的圖片比較一般,對付著看吧)

實(shí)現(xiàn)原理

自己封裝了個(gè)控件,它內(nèi)部含有一個(gè)Text控件,然后我們又寫了一個(gè)timer,然后負(fù)責(zé)倒計(jì)時(shí),然后每次都需要判斷一下是否繼續(xù),然后加了一個(gè)flag字段,判斷是否接受下次點(diǎn)擊事件,當(dāng)?shù)褂?jì)時(shí)結(jié)束之后還需要將初始狀態(tài)重置回去即可。

代碼

控件代碼

import React, {Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Image,
  TextInput,
  TouchableHighlight,
  StatusBar,
  Alert,
  AppRegistry
} from 'react-native';
import LinkRow from '../components/LinkRow';
import cStyles from '../styles/CommonStyle';

import axios from 'axios';

class MyCountTime extends Component {
  constructor(props) {
    super(props);
    let timeLeft = this.props.timeLeft > 0 ? this.props.timeLeft : 5;
    let width = this.props.width || 100;
    let height = this.props.height || 50;
    let color = this.props.color || '#42A5F5';
    let fontSize = this.props.fontSize || 30;
    let fontWeight = this.props.fontWeight || '600';
    let borderColor = this.props.borderColor || '#42A5F5';
    let borderWidth = this.props.borderWidth || 1;
    let borderRadius = this.props.borderRadius || 4;
    let backgroundColor = this.props.backgroundColor || '#42A5F5';
    let begin = 0;
    let press = this.props.press;

    this.afterEnd = this.props.afterEnd || this._afterEnd;
    this.style = this.props.style;

    this.state = {
      timeLeft: timeLeft,
      begin: begin
    };
    this.countTextStyle = {
      textAlign: 'center',
      color: '#42A5F5',
      fontSize: fontSize,
      fontWeight: fontWeight

    };
    this.countViewStyle = {
      backgroundColor: backgroundColor,
      alignItems: 'center',
      borderColor: borderColor,
      borderWidth: borderWidth,
      borderRadius: borderRadius,
      width: width,
      height: height
    }
  }

  countdownfn(timeLeft, callback, begin) {
    if (timeLeft > 0) {
      this.state.begin = 1;
      console.log("===lin===>");

      let that = this;
      let interval = setInterval(function () {
        if (that.state.timeLeft < 1) {
          clearInterval(interval);
          callback(that)
        } else {
          let totalTime = that.state.timeLeft;
          that.setState({
            timeLeft: totalTime - 1
          })
        }
      }, 1000)
    }
  }

  _beginCountDown() {
    if (this.state.begin === 1){
      return;
    }
    let time = this.state.timeLeft;
    console.log("===lin===> time " + time);
    let afterEnd = this.afterEnd;
    let begin = this.state.begin;
    console.log("===lin===> start " + begin);
    this.countdownfn(time, afterEnd, begin)
  }

  _afterEnd(that) {
    console.log('------------time over');
    that.setState({
      begin : 0,
      timeLeft : 5,
    })
  }

  componentDidMount() {

  }

  render() {
    return (
      <View style={{position:'absolute',top:13,right:43,height:30}}>
        <Text
          onPress={this._beginCountDown.bind(this)}
          style={{color: '#42A5F5', fontSize: 17,height:40 , zIndex:999}}> { this.state.begin === 0 ? '點(diǎn)擊獲取驗(yàn)證碼' : this.state.timeLeft} </Text>

      </View>
    )
  }
}

應(yīng)用代碼

<MyCountTime timeLeft={5}>

</MyCountTime>

當(dāng)然這只是,最簡單的應(yīng)用的代碼,我們還提供了很多的自定義的屬性,大家可以根據(jù)自己項(xiàng)目的需要,去調(diào)節(jié)這些參數(shù)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解在React里使用

    詳解在React里使用"Vuex"

    本篇文章主要介紹了詳解在React里使用"Vuex",小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • React 組件間的通信示例

    React 組件間的通信示例

    這篇文章主要介紹了React 組件間的通信示例,主要通信劃分為三種,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能

    react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能

    這篇文章主要介紹了react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 解決React報(bào)錯(cuò)Rendered more hooks than during the previous render

    解決React報(bào)錯(cuò)Rendered more hooks than during

    這篇文章主要為大家介紹了React報(bào)錯(cuò)Rendered more hooks than during the previous render解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React狀態(tài)管理Redux原理與介紹

    React狀態(tài)管理Redux原理與介紹

    redux是redux官方react綁定庫,能夠使react組件從redux store中讀取數(shù)據(jù),并且向store分發(fā)actions以此來更新數(shù)據(jù),這篇文章主要介紹了react-redux的設(shè)置,需要的朋友可以參考下
    2022-08-08
  • react子組件接收的props賦值給state的陷阱問題

    react子組件接收的props賦值給state的陷阱問題

    這篇文章主要介紹了react子組件接收的props賦值給state的陷阱問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 關(guān)于React中使用window.print()出現(xiàn)頁面無響應(yīng)問題解決記錄

    關(guān)于React中使用window.print()出現(xiàn)頁面無響應(yīng)問題解決記錄

    這篇文章主要介紹了React中使用window.print()出現(xiàn)頁面無響應(yīng)問題解決記錄,首先問題原因可能是操作了document但是并未進(jìn)行銷毀(可能是),具體問題解決思路參考下本文吧
    2021-11-11
  • React?+?Typescript領(lǐng)域初學(xué)者的常見問題和技巧(最新)

    React?+?Typescript領(lǐng)域初學(xué)者的常見問題和技巧(最新)

    這篇文章主要介紹了React?+?Typescript領(lǐng)域初學(xué)者的常見問題和技巧,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • react 父子組件之間通訊props

    react 父子組件之間通訊props

    這篇文章主要介紹了react 父子組件之間通訊props,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Webpack 4.x搭建react開發(fā)環(huán)境的方法步驟

    Webpack 4.x搭建react開發(fā)環(huán)境的方法步驟

    這篇文章主要介紹了Webpack 4.x搭建react開發(fā)環(huán)境的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08

最新評論

化德县| 普陀区| 万宁市| 中西区| 阳原县| 宣化县| 隆安县| 张家界市| 璧山县| 札达县| 缙云县| 图木舒克市| 石泉县| 来宾市| 沁源县| 招远市| 贵阳市| 科技| 清镇市| 绥芬河市| 准格尔旗| 江安县| 博野县| 井陉县| 邳州市| 常山县| 鄂托克旗| 郑州市| 沙坪坝区| 广安市| 菏泽市| 定安县| 泸溪县| 丰城市| 大港区| 彰化县| 晋城| 仁怀市| 桃源县| 汕头市| 凌源市|