const [buttonDisabled, setButtonDisabled] = useState(false);
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setButtonDisabled(true);
const response = await fetch(
"http://localhost:5000/....",
{
method: "POST",
body: JSON.stringify(state),
},
);
setButtonDisabled(false);
const responseJson = await response.text();
};
// Button
<button disabled={buttonDisabled} type="submit">Submit form</button>
I expect the button to get disabled (there is an indicator) during the fetching but it's not working. What am I doing wrong? Also is this an appropriate way to handle disabling the button? Or is there a better way.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…