- Progressive Web Apps with React
- Scott Domes
- 115字
- 2021-07-08 09:36:24
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.
推薦閱讀
- 程序員修煉之道:程序設計入門30講
- Learning Chef
- C語言程序設計基礎與實驗指導
- Python時間序列預測
- Python+Tableau數據可視化之美
- 細說Python編程:從入門到科學計算
- Cocos2d-x Game Development Blueprints
- 零基礎學HTML+CSS第2版
- MyBatis 3源碼深度解析
- Mastering JavaScript
- Modular Programming with JavaScript
- 軟件測試技術
- 計算機應用基礎(Windows 7+Office 2010)
- ASP.NET Core 2 High Performance(Second Edition)
- Oracle Database 12c DBA官方手冊(第8版)