gpt4 book ai didi

javascript - 如何改变状态?

转载 作者:行者123 更新时间:2023-12-01 00:48:43 24 4
gpt4 key购买 nike

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

如何将 handleSubmit 内的状态更改为 true - setMessageShown(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com