javascript - React Native - 为什么当我将 textInput 分配给 onchange 时单击按钮时才会出现错误?
问题描述
我在做一些项目时遇到了一些问题。
import React,{Component} from "react";
import {StyleSheet,Text,View,TextInput,
TouchableOpacity,Keyboard,KeyboardAvoidingView,Image} from "react-native";
import {reduxForm,Field} from "redux-form";
const validate = values =>{
const errors = {};
emailform = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i
if(!values.email){
errors.email="not correct format for email address"
}else
if(!emailform.test(values.email)){
errors.email="not correct format for email address"
}
if(!values.password){
errors.password="please use at least 6 - 12 characters"
}else
if(values.password.length < 6 || values.password.length > 12){
errors.password="please use at least 6 - 12 characters"
}
return errors;
}
const myField = ({label, placeholder ,meta:{error, touched}, input:{onChange}}) =>{
return(
<View>
<Text style={styles.inputlabel}> {(label)} </Text>
<TextInput
style={styles.input}
onChangeText={onChange}
/>
{touched && (error && (<Text style={styles.errormessage}>{error}</Text>))}
</View>
);
}
const submit = values =>{
alert('Login Success!');
}
const myFormCom = props=>{
const {handleSubmit, invalid} = props;
return(
<KeyboardAvoidingView behavior="padding" style={styles.container}>
<View style={styles.logoContainer}>
<Image
style={styles.logo}
source={require('./src/images/Logo.png')}
/>
</View>
<View style={styles.formContainer}>
<Field
name="email"
component={myField}
label="Email"
/>
<Field
name="password"
component={myField}
label="Password"
/>
<TouchableOpacity
style={styles.buttonContainer}
onPress={handleSubmit(submit)}
//disabled={invalid}
>
<Text
style={styles.btnLabel}>
Sign In
</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
)
}
const LoginForm = reduxForm({
form: 'something',
validate
})(myFormCom);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#faf8ff',
},
logoContainer:{
flexGrow: 1,
alignItems: 'center',
marginTop: 80,
},
logo:{
height: 100
},
formContainer:{
marginBottom: 30
},
input:{
height: 40,
backgroundColor: '#faf8ff',
marginLeft: 20,
marginRight: 20,
paddingLeft: 10,
borderWidth: 2,
borderColor: '#9d81d0',
borderRadius: 5
},
inputlabel:{
marginLeft: 20,
fontSize: 16,
fontWeight: '500',
marginBottom: 4
},
buttonContainer:{
backgroundColor: '#704db2',
marginLeft: 20,
marginRight: 20,
alignItems: 'center',
borderRadius: 5,
marginTop: 15
},
btnLabel:{
fontSize: 20,
color: '#fff',
fontWeight: 'bold',
padding: 10,
},
errormessage: {
color: 'red',
marginLeft: 20,
fontSize:14
}
});
export default LoginForm;
我想要的只是如果发生一些错误,按钮将被禁用,但当按钮未单击时,我的错误无法显示。那个怎么样?有人可以向我解释吗?抱歉,我在 react native 和 JavaScript 方面不太擅长;我还在学习我的东西哈哈
顺便说一句,我在 myField 中声明它在 textInput 中是 onChange ,所以如果文本发生变化,它会发生变化吗?但是为什么我的错误只在我点击登录按钮时才显示?即使尚未单击按钮,我如何才能正确显示错误。
解决方案
推荐阅读
- c# - WPF列表框不添加整个字符串
- postgresql - 让数据库身份验证在 Apache Guacamole 上工作
- java - Java PriorityQueue NPE 即使添加的元素不为空?
- sql-server - 数据库列数据类型从位更改为 char(1) SQL
- c++ - c ++ int指向数组指针数组的指针内存泄漏
- java - 空指针异常:com.google.android.gms.auth.api.GoogleSigninAccount(getIdtoken)
- r - R:按年份对离散时间序列进行分组
- python - 将不可打印的字节转换为字符串
- javascript - 如何使用 Angular 为 js-calender-year 中的每个选定日期添加颜色。页面刷新后颜色应保持不变
- python - 使用来自另一个 django 模型的输入数据