我有一個表單,我想在其中設定狀態(例如 setEducation)來自輸入的傳入值。我使用 onChange,但這會導致每次更改輸入欄位時都會呈現組件。解決問題的正確方法是什么?
<form id="loginform" onSubmit={onBecomeDoctorRequestHandler} >
<div className="form-group mb-3">
<label>Education</label>
<input
type="text"
className="form-control"
required
this onChange causes the placeholder="Harvard"
component to be rendered onChange={(event) => setEducation(event.target.value.trim())}
/>
<small className="text-danger form-text">
{educationError}
</small>
</div>
uj5u.com熱心網友回復:
當任何表單元素更改時,所有表單元素都將按原樣重新呈現,因為 onClick 的“值”是每次組件運行(呈現)時的新函式。
要解決這個問題,請將 onClick 函式包裝在 useCallback 中。useCallback 將在渲染程序中保留函式的“值”,因此只有需要重新渲染的元素才會保留。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/445790.html
