【发布时间】:2018-01-10 16:48:08
【问题描述】:
class LoginForm extends Component {
state = { email: '', password: '', alert: 'Please Enter Your Email And Password' }
onButtonPress() {
const { email, password } = this.state;
this.setState({ alert: 'Please Try Again' });
firebase.auth().signInWithEmailAndPassword(email, password)
.catch(() => {
firebase.auth().createUserWithEmailAndPassword(email, password)
.catch(() => {
this.setState({ alert: 'Login/Registeration Failed.' });
});
});
}
render() {
return (
<Card>
<CardSection>
<Button
text={'Login'}
onPress={this.onButtonPress.bind(this)}
正如您在最后一行代码中看到的,每当我的按钮被按下时,我的按钮都会调用 onButtonPress 函数。我还使用 .bind(this) 使方法绑定到 LoginForm 组件。因为据我所知,ES6 类不会自动将方法绑定到自身。但是,如果我写了 this.onButtonPress,方法 onButtonPress() 中的“this”指的是什么 而不是 this.onButtonPress.bind(this) 为什么?
【问题讨论】: