需要一些帮助才能使这更干燥。不确定是否使用switch语句...
.catch((err) => {
console.log(err)
let emailDup = 'user already exists'
let userDup = 'username already exists'
if (err.response.body.includes(emailDup)) {
this.setState({
errMessage: errEmailDup,
error: true,
loading: false
})
} else if (err.response.body.includes(userDup)) {
this.setState({
errMessage: errUserDup,
error: true,
loading: false
})
} else {
this.setState({
errMessage: errDefault,
error: true,
loading: false
})
}
}
答案 0 :(得分:3)
以下代码易于阅读和维护(添加更多案例也很容易):
.catch((err) => {
let emailDup = 'user already exists';
let userDup = 'username already exists';
let errMessage;
if (err.response.body.includes(emailDup)) {
errMessage = errEmailDup;
} else if (err.response.body.includes(userDup)) {
errMessage = errUserDup;
} else {
errMessage = errDefault;
}
this.setState({
errMessage: errMessage,
error: true,
loading: false
});
}
答案 1 :(得分:2)
您可以使用三元
FLOAT