如何正确地验证和重定向?



我正在React native中编写一些东西,必须处理检查数字是否正确匹配并导航到另一个屏幕

代码

const Redirect = () =>{
var otp_code;
if (otp_code =="123456"){
navigation.navigate('Profile');
}else{
Alert.alert('Invalid OTP');
}
}

而不是导航,即使我输入正确的数字作为123456,它仍然告诉我无效的OTP

完整的源代码如下:

import { StyleSheet, Text, View, Image, Alert, TouchableOpacity, TextInput } from "react-native";
import React , {useState, useMemo} from "react";
import {Picker} from '@react-native-picker/picker';
import { useNavigation } from "@react-navigation/native";

const VerifyNumber2 = () => {
const navigation = useNavigation();
const Redirect = () =>{
var otp_code;
if (otp_code =="123456"){
navigation.navigate('Profile');
}else{
Alert.alert('Invalid OTP');
}
}
const [otp, setOtp] = useState("");
return (
<View style={styles.container}>
<Image
style={{ width: 61, height: 61, top: 48, left: 177 }}
source={require("../assets/*.png")}
/>
<Text style={styles.hometext}>Verify your Phone Number</Text>
<Text style={styles.hometext2}>* will need to verify your PhoneNumber</Text>
<View style={{flexDirection :'row', alignItems : 'center', justifyContent :'space-evenly', paddingHorizontal:8}}>
<TextInput
value={otp}
placeholder = "Enter OTP"
onChangeText={(otp) => setOtp(otp)}
style={styles.input}
maxLength={15} 
/>
</View>
<View>
<TouchableOpacity onPress={()=> Redirect()}>  
<View style={styles.button}>  
<Text style={styles.buttonText}>Next</Text>  
</View>  
</TouchableOpacity>  
</View>

</View>
);
};
export default VerifyNumber2;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "white",
width: "100%",
height: "100%",
},
hometext:{
marginTop : 80,
textAlign : 'center',
fontWeight : 'bold',
fontSize : 20
},
hometext2:{
flexShrink : 1,
marginTop : 10,
flexWrap : "wrap",
textAlign : 'center',
fontWeight : 400
},
button:{
width : 282,
height : 41,
top : 120,
left : 66,
borderRadius : 10,
backgroundColor : '#400080'
},
buttonText:{
fontWeight : "bold",
color : '#FFFF',
textAlign : 'center',
top : 10
},
input:{
marginTop : 50,
padding: 5,
backgroundColor : '#D9D9D957',
width : 282,
height : 46,
borderRadius : 10
},
picker :{
top : 40,
height: 41, 
width: 282,
alignSelf : 'center',
borderRadius : 10,
backgroundColor : '#D9D9D957'
}
});

我想知道我做错了什么?请帮助

我解决了。

声明后

const [otp, setOtp] = useState("");

没有必要把它写成var otp

所以我检查了它,它重定向没有问题。谢谢大家

最新更新