React-hooks-form 中的受控组件和非受控组件之间的主要区别是什么?

Pra*_*ini 0 javascript reactjs react-hook-form

我正在使用反应钩子形式。我从有关受控和非受控的文档中读到。

受控

<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>
Run Code Online (Sandbox Code Playgroud)

这是不受控制的形式

<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>
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我这有什么区别吗?通过制造受控组件而不是不受控组件我可以获得什么?

Bru*_*uce 11

React Hook Form 包含不受控制的表单和输入,这意味着您仍然可以构建受控的表单和输入:https://twitter.com/bluebill1049/status/1286438673546768386

ref={register}那么和之间有什么区别Controller