如何解决验证错误消息在本机反应中没有改变
我正在学习如何使用 yup 验证,这是我的代码。 但验证错误消息不会改变。
import React from "react";
import { StyleSheet,Image } from "react-native";
import * as Yup from "yup";
import { Formik } from "formik";
import AppTextInput from "../components/AppTextInput";
import Screen from "../components/Screen";
import AppButton from "../components/AppButton";
import ErrorMessage from "../components/ErrorMessage";
const validationSchema = Yup.object().shape({
email: Yup.string().required().email("Please enter a valid email").label("Email"),password: Yup.string().required().min(4,"To short").label("Password"),});
function LoginScreen() {
return (
<Screen style={styles.container}>
<Image style={styles.logo} source={require("../assets/logo-red.png")} />
<Formik
initialValues={{ email: "",password: "" }}
onSubmit={(values) => console.log(values)}
validationSchema={validationSchema}
>
{({ handleChange,handleSubmit,errors,setFieldTouched,touched }) => (
<>
<AppTextInput
autoCapitalize="none"
autoCorrect={false}
icon="email"
keyboardType="email-address"
onBlur={() => setFieldTouched("email")}
name="email"
placeholder="Email"
textContentType="emailAddress"
/>
<ErrorMessage visible={touched.email} error={errors.email} />
<AppTextInput
autoCapitalize="none"
autoCorrect={false}
icon="lock"
name="password"
placeholder="Password"
onBlur={() => setFieldTouched("password")}
secureTextEntry
textContentType="password"
/>
<ErrorMessage visible={touched.password} error={errors.password} />
<AppButton onPress={handleSubmit} title="Login" />
</>
)}
</Formik>
</Screen>
);
}
const styles = StyleSheet.create({
container: {
padding: 10,},logo: {
width: 80,height: 80,alignSelf: "center",marginTop: 50,marginBottom: 20,});
export default LoginScreen;
解决方法
您没有编写 onChangeText
道具
像这样写两个AppTextInput
<AppTextInput
autoCapitalize="none"
autoCorrect={false}
onChangeText={handleChange('email')} // This was missing
icon="email"
keyboardType="email-address"
onBlur={() => setFieldTouched('email')}
name="email"
placeholder="Email"
textContentType="emailAddress"
/>
<AppTextInput
autoCapitalize="none"
autoCorrect={false}
onChangeText={handleChange('password')} // This was missing
icon="lock"
name="password"
placeholder="Password"
onBlur={() => setFieldTouched('password')}
secureTextEntry
textContentType="password"
/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。