官术网_书友最值得收藏!

Code cleanup

Our handleSubmit function is getting a little long and difficult to follow. Let's do some reorganization before we move on.

We'll start by moving everything after the initial if statement inside a separate function, called login(), for simplicity:

login() {
firebase
.auth()
.signInWithEmailAndPassword(this.state.email, this.state.password)
.then(res => {
console.log(res);
})
.catch(err => {
if (err.code === 'auth/user-not-found') {
this.signup();
} else {
this.setState({ error: 'Error logging in.' });
}
});
}

Then, our handleSubmit becomes much smaller:

handleSubmit = event => {
event.preventDefault();
this.setState({ error: '' });
if (this.state.email && this.state.password) {
this.login();
} else {
this.setState({ error: 'Please fill in both fields.' });
}
};

It's much easier to read and follow now.

主站蜘蛛池模板: 札达县| 容城县| 阳山县| 太仓市| 西乌| 眉山市| 牡丹江市| 清新县| 阿坝| 巨野县| 太原市| 西昌市| 南宫市| 南安市| 彭阳县| 新建县| 内江市| 体育| 磐石市| 阳春市| 禄丰县| 浑源县| 祥云县| 米林县| 易门县| 大渡口区| 华宁县| 永登县| 东乡族自治县| 泌阳县| 濮阳县| 兴山县| 肃宁县| 辉南县| 永济市| 龙川县| 克什克腾旗| 昭通市| 庆安县| 雅江县| 冀州市|