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

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.

主站蜘蛛池模板: 嘉鱼县| 平利县| 集贤县| 沙洋县| 宣恩县| 抚顺县| 泽普县| 安塞县| 平顶山市| 宁乡县| 江源县| 莎车县| 昌图县| 延长县| 怀仁县| 赫章县| 濮阳县| 延庆县| 新田县| 汕头市| 遵义县| 哈密市| 嵊州市| 九龙坡区| 台中县| 崇礼县| 鹤壁市| 江口县| 布拖县| 枣强县| 阿拉善盟| 安庆市| 客服| 外汇| 平凉市| 攀枝花市| 兴业县| 涟源市| 乐平市| 泰来县| 桦甸市|