【问题标题】:How to build a OTP Component in React native with counter timer and resend otp functionality using functional component [duplicate]如何使用计数器计时器在 React Native 中构建 OTP 组件并使用功能组件重新发送 otp 功能 [重复]
【发布时间】:2019-11-19 07:44:20
【问题描述】:

我已经尝试了几篇文章来构建可自定义但运气不好的 OTP 组件

其中一些在IOS平台上运行,但不在android上运行

最后,作为一个新手,经过一番努力,我完成了 react-native

【问题讨论】:

    标签: javascript reactjs react-native react-hooks one-time-password


    【解决方案1】:

    这是使用 React 功能组件和 React hooks 完成的

    yarn add react-native-confirmation-code-input
    yarn add react-native-countdown-component
    yarn add react-redux
    
    
    import React, { useState, useRef } from "react";
    import {
      View,
      StyleSheet,
      Text,
      TextInput,
      TouchableOpacity,
      Alert
    } from "react-native";
    import CountDown from 'react-native-countdown-component';
    import CodeInput from 'react-native-confirmation-code-input';
    import Icon from "react-native-vector-icons/FontAwesome5";
    import { Input, Button } from "react-native-elements";
    import { useSelector, useDispatch } from "react-redux";
    import * as AuthActions from "../store/actions/auth";
    
    export const OtpVerifyScreen = props => {
      const auth = useSelector(state => state.auth);
      const dispatch = useDispatch();
      const inputRef = useRef('codeInputRef2');
      const [counter, SetCounter] = useState(150); // Set here your own timer configurable
      const [random, SetRandom] = useState(Math.random());
      const [disabled, setDisabled] = useState(true)
      const handleResend = () => {
        SetRandom(Math.random())
        // Handle Resend otp action here
      }
      const handleVerify = (otp) => {
      // Handle the verification logic here
      // dispatch verify action
      };
    
      return (
        <View style={styles.container}>
          <Text style={{ padding: 10 }}>Enter OTP</Text>
          <View style={styles.otp}>
            <View style={{ height: 100, width: 200, marginLeft: 10 }}>
              <CodeInput
                ref={inputRef}
                // secureTextEntry
                className={'border-b'}
                activeColor='rgba(0, 0, 0, 1)'
                inactiveColor='rgba(0, 0, 0, 1)'
                space={10}
                keyboardType="numeric"
                autoFocus={true}
                codeLength={6}
                size={20}
                inputPosition='left'
                onFulfill={(code) => handleVerify(code)}
              />
            </View>
            <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'center' }}>
              <CountDown
                key={random}
                until={counter}
                size={15}
                onFinish={() => setDisabled(() => false)}
                separatorStyle={{ color: 'black' }}
                digitStyle={{ backgroundColor: '#FFF' }}
                digitTxtStyle={{ color: 'black' }}
                timeToShow={['M', 'S']}
                showSeparator
                timeLabels={{ m: '', s: '' }}
              />
              <Button disabled={disabled} style={{ marginLeft: 10 }} title="RESEND" onPress={handleResend}></Button>
            </View>
          </View>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "flex-start",
        marginTop: 20,
      },
      otp: {
        flex: 1,
        justifyContent: "flex-start",
        alignItems: "flex-start"
      }
    });
    
    export default OtpVerifyScreen;
    
    

    希望这会有所帮助 编码快乐!!

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 2022-11-22
      • 2020-03-25
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2021-06-14
      • 2019-08-04
      • 2020-08-12
      相关资源
      最近更新 更多