export default class Login extends React.Component {
static navigationOptions = {
title: 'Welcome',
header: null
};
constructor(props) {
super(props);
state = {
email: "",
password: ""
}
}
handleEmail = (text) => {
this.setState({
email: text
})
}
handlePassword = (text) => {
this.setState({
password: text
})
}
validEmail = Email => {
var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9] {
2,
4
}) + $ /
return email.test(Email)
}
onClickListener = (viewId) => {
Alert.alert("Alert", "Button pressed " + viewId);
}
onSubmit() {
if (this.state.email === "" || this.state.email === null) {
alert("Email cannot be empty")
} else if (!this.validEmail(this.state.email)) {
alert("Enter valid Mail id")
} else if (this.state.password === "" || this.state.password === null) {
alert("Password cannot be empty")
} else if (this.state.password.length < 6) {
alert("Password should contain atleast 6 characters")
} else {
alert("success")
this.props.navigation.navigate('ScrollTab');
}
}
render() {
return ( <
View style = {
styles.container
} >
<
Text style = {
styles.LogoText
} > Blood Donation App < /Text> <
View style = {
styles.inputContainer
} >
<
TextInput style = {
styles.inputs
}
placeholder = "Email"
keyboardType = "email-address"
onChangeText = {
(text) => this.handleEmail(text)
}
underlineColorAndroid = 'transparent' / >
<
Image style = {
styles.inputIcon
}
source = {
{
uri: 'https://img.icons8.com/nolan/40/000000/email.png'
}
}
/> <
/View> <
View style = {
styles.inputContainer
} >
<
TextInput style = {
styles.inputs
}
placeholder = "Password"
secureTextEntry = {
true
}
onChangeText = {
(text) => this.handlePassword(text)
}
underlineColorAndroid = 'transparent' / >
<
Image style = {
styles.inputIcon
}
source = {
{
uri: 'https://img.icons8.com/nolan/40/000000/key.png'
}
}
/> <
/View> <
TouchableOpacity style = {
styles.btnForgotPassword
}
onPress = {
() =>
this.onClickListener('restore_password')
} >
<
Text style = {
styles.btnText
} > Forgot your password ? < /Text> <
/TouchableOpacity> <
TouchableOpacity style = {
[styles.buttonContainer,
styles.loginButton
]
}
onPress = {
() => this.onSubmit()
} >>
<
Text style = {
styles.loginText
} > Login < /Text> <
/TouchableOpacity> <
/View>
);
}
}
//正在验证电子邮件和密码。 如果直接单击提交,则显示错误,该错误为null未定义。 我应该将状态设置为一些默认值吗? 错误是:TypeError:TypeError:null不是对象(正在评估 'this.state.email') 提交错误 如果我在其中添加value = {this.state.email} 给出错误,即未定义null
答案 0 :(得分:3)
在您的构造函数中将此状态插入状态之前。
this.state = {...}
答案 1 :(得分:2)
状态可以在react的statefull / class组件中以两种方式声明
内部构造函数:
constructor(props) {
super(props);
this.state = {
email: "",
password: ""
}
}
内部类和外部构造函数:
state = {
email: "",
password: ""
}
您需要为TextInput元素添加值prop
更改
<TextInput style={styles.inputs}
placeholder="Email"
keyboardType="email-address"
onChangeText={(text) => this.handleEmail(text)}
underlineColorAndroid='transparent'/>
收件人
<TextInput style={styles.inputs}
placeholder="Email"
keyboardType="email-address"
value={this.state.email}
onChangeText={email => this.handleEmail(email)}
underlineColorAndroid='transparent'/>
并按如下所示设置电子邮件
handleEmail = email => {
this.setState({
email: email
})
}