- 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.