- 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.
推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Facebook Application Development with Graph API Cookbook
- Monkey Game Development:Beginner's Guide
- 測試驅動開發:入門、實戰與進階
- 摩登創客:與智能手機和平板電腦共舞
- 區塊鏈架構與實現:Cosmos詳解
- Object-Oriented JavaScript(Second Edition)
- Building Mapping Applications with QGIS
- 深度學習:算法入門與Keras編程實踐
- Python貝葉斯分析(第2版)
- Apex Design Patterns
- Hands-On Functional Programming with TypeScript
- Swift Playgrounds少兒趣編程
- Python3.5從零開始學
- OpenStack Networking Essentials