[英]React-final-form focus doesn't focus on given field
正如@erikras 在GitHub 問題中的回應:
這不是 form.focus() 所做的。 form.focus() 告訴 Final Form 某個字段已獲得焦點。
您需要對 DOM 節點的實際引用。 像這樣: https : //codesandbox.io/s/mystifying-mestorf-26yv8?file=/src/App.jsx
從代碼沙盒中,我們應該const ref = useRef();
帶輸入
<Field name="firstName" type="text">
{({ input }) => (
<input {...input} placeholder="First Name" ref={ref} />
)}
</Field>
隨后onClick
:
<button
type="button"
onClick={() => {
ref.current.focus();
}}
disabled={submitting || pristine}
>
Focus
</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.