英文:
Is there something wrong with my switch statement?
问题
The function handleForm
is triggered when the button is clicked, and it is supposed to check all the inputs.
const [errorMessage, setErrorMessage] = useState({
lastName: "",
firstName: "",
lastNameHiragana: "",
firstNameHiragana: "",
birthday: "",
sex: "",
telephone: "",
email: "",
joinedDate: "",
division: "",
});
const [values, setValues] = useState({
lastName: "",
firstName: "",
lastNameHiragana: "",
firstNameHiragana: "",
birthday: "",
sex: "",
telephone: "",
email: "",
joinedDate: "",
division: "",
});
function handleForm(e) {
e.preventDefault();
for (const name in values) {
const hiraganaRegex = /^[ぁ-ん]+$/;
switch (name) {
case "lastName":
if (!values[name]) {
setErrorMessage({ ...errorMessage, lastName: "必須項目です" });
}
break;
case "firstName":
if (!values[name]) {
setErrorMessage({ ...errorMessage, firstName: "必須項目です" });
} else {
setErrorMessage({ ...errorMessage, firstName: "" });
}
break;
case "lastNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
setErrorMessage({
...errorMessage,
lastNameHiragana: "ひらがなで入力して下さい。",
});
}
break;
case "firstNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
setErrorMessage({
...errorMessage,
firstNameHiragana: "ひらがなで入力して下さい。",
});
}
break;
case "birthday":
if (values[name]) {
setErrorMessage({ ...errorMessage, birthday: "" });
}
break;
}
}
}
When I test this, the setErrorMessage
of the first case does not trigger. I check the state in the dev tools, and it does not update. It only updates the second case. I inserted a console.log
in the first case (inside the if
statement), and it logs, proving that it is getting there. But somehow it is not updating the errorMessage
.
Is there something wrong in my code (syntax, etc.)? Somebody knows what is wrong here? Any help would be much appreciated. Thanks in advance.
英文:
The function handleForm
is triggered when the button is clicked, and it is supose to check all the inputs.
const [errorMessage, setErrorMessage] = useState({
lastName: "",
firstName: "",
lastNameHiragana: "",
firstNameHiragana: "",
birthday: "",
sex: "",
telephone: "",
email: "",
joinedDate: "",
division: "",
});
const [values, setValues] = useState({
lastName: "",
firstName: "",
lastNameHiragana: "",
firstNameHiragana: "",
birthday: "",
sex: "",
telephone: "",
email: "",
joinedDate: "",
division: "",
});
function handleForm(e) {
e.preventDefault();
for (const name in values) {
const hiraganaRegex = /^[ぁ-ん]+$/;
switch (name) {
case "lastName":
if (!values[name]) {
setErrorMessage({ ...errorMessage, lastName: "必須項目です" });
}
break;
case "firstName":
if (!values[name]) {
setErrorMessage({ ...errorMessage, firstName: "必須項目です" });
} else {
setErrorMessage({ ...errorMessage, firstName: "" });
}
break;
case "lastNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
setErrorMessage({
...errorMessage,
lastNameHiragana: "ひらがなで入力して下さい。",
});
}
break;
case "firstNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
setErrorMessage({
...errorMessage,
firstNameHiragana: "ひらがなで入力して下さい。",
});
}
break;
case "birthday":
if (values[name]) {
setErrorMessage({ ...errorMessage, birthday: "" });
}
break;
}
}
}
When I test this, the setErrorMessage
of the first case does not trigger. I check the state in the dev tools, and it does not update.
It only updates the second case.
I inserted a console.log
in the first case (inside the if
statement), and it logs, proving that it is getting there. But somehow it is not updating the errorMessage
.
Is there something wrong in my code (syntax, etc.)?
Somebody knows what is wrong here?
Any help would be much appreciated.
Thanks in advance
答案1
得分: 1
It looks like you are not correctly updating the errorMessage
state in the first case of your switch statement. Instead of spreading the current errorMessage
state, you are overwriting it with the new value.
这似乎是解决方法:
case "lastName":
if (!values[name]) {
setErrorMessage((prevState) => ({ ...prevState, lastName: "必須項目です" }));
} else {
setErrorMessage((prevState) => ({ ...prevState, lastName: "" }));
}
break;
英文:
Seems like you are not updating the errorMessage
state correctly in the first case of your switch statement. Instead of spreading the current errorMessage
state, you are overwriting it with the new value.
This may fix that:
case "lastName":
if (!values[name]) {
setErrorMessage((prevState) => ({ ...prevState, lastName: "必須項目です" }));
} else {
setErrorMessage((prevState) => ({ ...prevState, lastName: "" }));
}
break;
答案2
得分: 1
循环内部,你正在覆写整个errorMessage
对象。应该只更新正在检查的特定输入的特定错误消息。
你可以使用input name
作为key
分别更新每个错误消息:
英文:
Inside the loop you are overwriting the entire errorMessage
object. Instead of updating just that specific error message for just that input being checked.
You could update each error message separately using the input name
as the key
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function handleForm(e) {
e.preventDefault();
const newErrorMessage = {};
for (const name in values) {
const hiraganaRegex = /^[ぁ-ん]+$/;
switch (name) {
case "lastName":
if (!values[name]) {
newErrorMessage[name] = "必須項目です";
}
break;
case "firstName":
if (!values[name]) {
newErrorMessage[name] = "必須項目です";
} else {
newErrorMessage[name] = "";
}
break;
case "lastNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
newErrorMessage[name] = "ひらがなで入力して下さい。";
}
break;
case "firstNameHiragana":
if (!hiraganaRegex.test(values[name]) && values[name]) {
newErrorMessage[name] = "ひらがなで入力して下さい。";
}
break;
case "birthday":
if (values[name]) {
newErrorMessage[name] = "";
}
break;
}
}
setErrorMessage(newErrorMessage);
}
<!-- end snippet -->
By using the newErrorMessage
it will update each error message inside the loop. After the loop is finished, we call setErrorMessage
with the newErrorMessage
object, which will update the error messages in the state.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论