“我的 switch 语句有问题吗?”

huangapple go评论70阅读模式
英文:

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 &quot;lastName&quot;:
if (!values[name]) {
newErrorMessage[name] = &quot;必須項目です&quot;;
}
break;
case &quot;firstName&quot;:
if (!values[name]) {
newErrorMessage[name] = &quot;必須項目です&quot;;
} else {
newErrorMessage[name] = &quot;&quot;;
}
break;
case &quot;lastNameHiragana&quot;:
if (!hiraganaRegex.test(values[name]) &amp;&amp; values[name]) {
newErrorMessage[name] = &quot;ひらがなで入力して下さい。&quot;;
}
break;
case &quot;firstNameHiragana&quot;:
if (!hiraganaRegex.test(values[name]) &amp;&amp; values[name]) {
newErrorMessage[name] = &quot;ひらがなで入力して下さい。&quot;;
}
break;
case &quot;birthday&quot;:
if (values[name]) {
newErrorMessage[name] = &quot;&quot;;
}
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.

huangapple
  • 本文由 发表于 2023年5月6日 18:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定