我正在使用反应挂钩形式。我从有关受控和不受控的文档中阅读。
受控
<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>
有人可以告诉它有什么区别吗?通过制造受控组件而不是不受控制的组件,我可以获得什么?
素胚勾勒不出你
相关分类