我需要解决这些错误。
首先,如果我单击登录按钮,则会使我返回错误=> _this2.state不是函数
和
第二,如果我单击注册按钮,则会使我退回错误=> _this3.state不是函数
如何解决这个问题?
export default class LoginScreen extends React.Component {
constructor(props){
super(props);
this.state = {
email: '',
password: '',
error: '',
loading: false,
};
this.onLoginPress = this.onLoginPress.bind(this);
this.onSignUpPress = this.onSignUpPress.bind(this);
}
onLoginPress() {
this.setState({ error:'', loading:true });
const {email, password} = this.state;
firebase.auth().signInWithEmailAndPassword(email, password)
.then(()=> {
this.state({error:'', loading:false});
this.props.navigation.navigate('Profile');
})
.catch(()=>{
this.state({error:'Authentication Failed!', loading:false})
})
}
onSignUpPress() {
this.setState({ error:'', loading:true });
const {email, password} = this.state;
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(()=> {
this.state({error:'', loading:false});
this.props.navigation.navigate('Profile');
})
.catch(()=>{
this.state({error:'Authentication Failed!', loading:false})
})
}
renderButtonOrLoading(){
if( this.state.loading ){
return <Text>Loading ...</Text>
} else {
return(
<View>
<Button
title="Login"
onPress={this.onLoginPress}/>
<Button
title="Sign Up"
onPress={this.onSignUpPress}/>
</View>
);
}
}
render() {
return(
<View>
<FormLabel>Email</FormLabel>
<FormInput onChangeText={email => this.setState({email})}/>
<FormLabel>Password</FormLabel>
<FormInput onChangeText={password => this.setState({password})}/>
<FormValidationMessage>Error Message</FormValidationMessage>
{ this.renderButtonOrLoading() }
</View>
);
}
}
答案 0 :(得分:0)
尝试将this.state
和onLoginPress
中的onSignUpPress
更改为this.setState
onLoginPress() {
this.setState({ error:'', loading:true });
const {email, password} = this.state;
firebase.auth().signInWithEmailAndPassword(email, password)
.then(()=> {
// Fix below line
this.setState({error:'', loading:false});
this.props.navigation.navigate('Profile');
})
.catch(()=>{
// Fix below line
this.setState({error:'Authentication Failed!', loading:false})
})
}
onSignUpPress() {
this.setState({ error:'', loading:true });
const {email, password} = this.state;
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(()=> {
// Fix below line
this.setState({error:'', loading:false});
this.props.navigation.navigate('Profile');
})
.catch(()=>{
// Fix below line
this.setState({error:'Authentication Failed!', loading:false})
})
}
答案 1 :(得分:0)
让我知道它是否有效。