[英]react-hook-form is leaving the input field while typing with mode property set
[英]Input field uppercase while typing react hook form
我向一個使用 react-hook-form v.3 的舊項目添加了一個邏輯 長話短說,當用戶在輸入字段中鍵入時,字母必須是大寫的。
這是我需要更改的輸入代碼:
// outside the form
const [value, setValue] = useState(data.taxCode ?? '')
const changeHandler = ({ target }) => {
setValue(target.value.toUpperCase())
}
// inside the form
<input
type='text'
name='taxCode'
value={value}
onChange={changeHandler}
ref={register({
pattern: {
value: taxCodeRgx,
message: 'error message,
},
})}
/>
它有效。 我有一個小問題:如果我雙擊輸入字段和 select 以前輸入的代碼,它不會 select 它.. 在第二次嘗試是。 此外,當我輸入時,第一個字符丟失任何想法? 謝謝
解決了:
我需要將模式設置為“onChange”: const methods = useForm({ mode: 'onChange' })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.