我想在匯總表單時使用 useState 在我的按鈕中設定掛起狀態,但我不知道我在哪里做錯了:
這是我的代碼:
const [is_pending, setIs_pending] = useState(false);
const SubmitContact = async (e)=>{
e.preventDefault()
setIs_pending = true;
let url = "http://localhost:8000/my-api/"
const res = await axios.post(url,{
contact_name:contact_name,contact_email:contact_email,contact_body:contact_body
}).then((res)=>{
console.log(res)
setIs_pending = false;
});
<form onSubmit={SubmitContact}>
...my others html code
{ !is_pending && <button className='btn btn-primary'>Send Message</button> }
{ is_pending && <button type='submit' className='btn btn-primary' disabled>Sending Message....</button> }
</form>
我設定 { is_pending && <button type='submit' className='btn btn-primary' disabled>Sending Message....</button> }了表格何時進行游行,但我不知道為什么在提交表格時沒有顯示此狀態。
uj5u.com熱心網友回復:
setIs_pending是一個函式,你不能使用=!
要正確設定值,您需要將值作為引數傳遞setIs_pending,例如:
YOUR_CODE....
setIs_pending(true);
YOUR_CODE....
你可以在這里閱讀更多內容:https ://pt-br.reactjs.org/docs/hooks-intro.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/487289.html
標籤:javascript 反应
上一篇:如何在JavascriptES6中宣告類meber變數?
下一篇:添加顯示以有條件地反應組件?
