我有一个表单,当我单击按钮时,我必须显示消息。消息有两种状态-setMessageShown(true);和setMessageShown(true);

如何在handleSubmit-setMessageShown(true);中将状态更改为true?

const handleSubmit = e => {
  e.preventDefault();
  // ...
};

const ReviewPopup = ({ name }) => {
  const [messageShown, setMessageShown] = useState(false);
  const hideMessage = () => {
    setMessageShown(false);
  };

  return (
    <form onSubmit={handleSubmit} className="new_review" id="new_review" action="/reviews" acceptCharset="UTF-8" method="post">
    // ...
    </form>
  );
};

最佳答案

将handleSubmit放入组件中。

const ReviewPopup = ({ name }) => {
  const [messageShown, setMessageShown] = useState(false);
  const hideMessage = () => {
    setMessageShown(false);
  };

  function handleSubmit(e) {
    e.preventDefault();
    setMessageShown(true);
  }

  return (
    <form onSubmit={handleSubmit} className="new_review" id="new_review" action="/reviews" acceptCharset="UTF-8" method="post">
    // ...
    </form>
  );
};

关于javascript - 如何改变状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57161555/

10-12 01:50