米琪卡哇伊
2022-12-22 12:55:47
我正在使用反應(yīng)掛鉤形式。我從有關(guān)受控和不受控的文檔中閱讀。受控<form onSubmit={handleSubmit(onSubmit)}> <input name="firstName" ref={register({ required: true })} /> <input name="lastName" ref={register} /> <input type="reset" /> // standard reset button <input type="button" onClick={reset} /> <input type="button" onClick={() => reset({ firstName: "bill" }); }} /> // reset form with values <input type="button" onClick={() => { reset({ firstName: "bill" }, { errors: true, // errors will not be reset dirtyFields: true, // dirtyFields will not be reset isDirty: true, // dirty will not be reset isSubmitted: false, touched: false, isValid: false, submitCount: false, }); }} /></form>這是不受控制的形式<form onSubmit={handleSubmit(onSubmit)}> <Controller as={TextField} name="firstName" control={control} rules={ required: true } defaultValue="" /> <Controller as={TextField} name="lastName" control={control} defaultValue="" /> <input type="submit" /> <input type="button" onClick={reset} /> <input type="button" onClick={() => { reset({ firstName: "bill", lastName: "luo" }); }} /></form>有人可以告訴它有什么區(qū)別嗎?通過(guò)制造受控組件而不是不受控制的組件,我可以獲得什么?
1 回答

素胚勾勒不出你
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
React Hook Form 包含不受控的表單和輸入,這意味著您仍然可以構(gòu)建受控的表單和輸入:https ://twitter.com/bluebill1049/status/1286438673546768386
ref={register}
那么和之間有什么區(qū)別Controller
?
ref={register}
:https ://react-hook-form.com/api#register表示不受控制的輸入將訂閱輸入更改并通過(guò) react-hook-form 檢索其值。Controller
:https ://react-hook-form.com/api#Controller是一個(gè)包裝器組件,它隔離受控組件以在其范圍內(nèi)重新呈現(xiàn),從而減少對(duì)應(yīng)用程序/表單級(jí)別的性能影響。
添加回答
舉報(bào)
0/150
提交
取消