gpt4 book ai didi

javascript - 如何在 React Native 中构建带有计数器计时器的 OTP 组件,并使用功能组件重新发送 OTP 功能

转载 作者:行者123 更新时间:2023-12-02 22:30:16 26 4
gpt4 key购买 nike

我已经尝试过几篇文章来构建可定制的 OTP 组件,但运气不好

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

作为一个 React-Native 新手,经过一番努力之后,我最终完成了这个任务

最佳答案

这是使用 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;

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

关于javascript - 如何在 React Native 中构建带有计数器计时器的 OTP 组件,并使用功能组件重新发送 OTP 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com