类型错误: 类型错误: null 不是对象(计算"this.state.email")


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未定义的错误。我应该将state设置为某个默认值吗?错误为:TypeError:TypeError:null不是对象(正在评估'this.state.email')onsubmit错误如果我在其中添加值={this.state.email}也是给出null为未定义的错误

状态可以通过两种方式在react 中的statefull/class组件中声明

  1. 内部构造函数
  2. 内部类和外部构造函数

内部构造函数:

constructor(props) {
super(props);
this.state = {
email: "",
password: ""
}
}

内部类和外部构造函数:

state = {
email: "",
password: ""
}

您需要将value prop添加到TextInput元素中,以便

更改

<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
})
}

在构造函数中插入this before state。

this.state = {...}

相关内容

  • 没有找到相关文章

最新更新