类型错误:类型错误:null 不是对象(正在评估“this.state.email”)
2019-02-01
6357
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>
);
}
}
// 正在验证电子邮件和密码。 如果直接单击 onsubmit,则会显示错误,即 null 未定义。 我应该将状态设置为某个默认值吗? 错误是:TypeError:TypeError:null 不是对象(正在评估 'this.state.email') onsubmit 错误 并且如果我在其中添加 value={this.state.email},也会 给出错误,即 null 未定义
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
})
}
Hemadri Dasari
2019-02-01
在您的构造函数中插入此之前状态。
this.state = {...}
Mattia Gecchele
2019-02-01