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

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.

主站蜘蛛池模板: 庆城县| 阿瓦提县| 荣成市| 安泽县| 乡宁县| 屯留县| 镇沅| 周口市| 保定市| 牡丹江市| 大丰市| 南川市| 鹰潭市| 井冈山市| 金堂县| 翁牛特旗| 雷山县| 土默特右旗| 大洼县| 宜城市| 泸定县| 永宁县| 建水县| 华亭县| 库尔勒市| 正蓝旗| 宝清县| 余江县| 溧水县| 北宁市| 讷河市| 高尔夫| 旬邑县| 通城县| 惠东县| 昌乐县| 吉安市| 应用必备| 得荣县| 漳浦县| 定州市|