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

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.

主站蜘蛛池模板: 巨鹿县| 永仁县| 贡山| 奉化市| 钟祥市| 宁津县| 上林县| 泗水县| 陇川县| 高雄县| 大英县| 温宿县| 衡水市| 山丹县| 双峰县| 平舆县| 昌图县| 通渭县| 郴州市| 平定县| 普兰县| 卫辉市| 曲沃县| 大足县| 五台县| 贺兰县| 温州市| 邵东县| 乌苏市| 偃师市| 江达县| 太谷县| 莆田市| 烟台市| 南充市| 仲巴县| 花莲市| 高陵县| 陕西省| 安康市| 赣州市|