亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

react-hooks-form 中的受控組件和非受控組件之間的主要區別是什么?

react-hooks-form 中的受控組件和非受控組件之間的主要區別是什么?

米琪卡哇伊 2022-12-22 12:55:47
我正在使用反應掛鉤形式。我從有關受控和不受控的文檔中閱讀。受控<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>有人可以告訴它有什么區別嗎?通過制造受控組件而不是不受控制的組件,我可以獲得什么?
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

React Hook Form 包含不受控的表單和輸入,這意味著您仍然可以構建受控的表單和輸入:https ://twitter.com/bluebill1049/status/1286438673546768386

ref={register}那么和之間有什么區別Controller

  • ref={register}https ://react-hook-form.com/api#register表示不受控制的輸入將訂閱輸入更改并通過 react-hook-form 檢索其值。

  • Controllerhttps ://react-hook-form.com/api#Controller是一個包裝器組件,它隔離受控組件以在其范圍內重新呈現,從而減少對應用程序/表單級別的性能影響。


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號