[英]React Hook Form reset Controller (value) to default
Here is an example of my component, wrapped into Controller这是我的组件的一个示例,包裹在 Controller 中
<div className="mt-1">
<Controller
control={control}
name="selectedBirthMonth"
defaultValue={selectedMonth}
render={({ field }) => (
< SelectBirthMonth
field={field}
startYear={startYear}
selectedYear={selectedYear}
months={months}
value={selectedMonth}
reducedMonths={reducedMonths}
onChange={useEffect(() => monthSelect(field.value)), [selectedMonth]}/>
)}
/>
</div>
For now I am just trying to reset it with a button.现在我只是想用一个按钮来重置它。 Later I'd love to conditionally reset, depending on the value in a sibling component.
后来我喜欢有条件地重置,这取决于兄弟组件中的值。 Would be great to get it work with the button in the first place:
首先让它与按钮一起工作会很棒:
<input
style={{ display: "block", marginTop: 20 }}
type="button"
onClick={() =>
reset(selectedBirthMonth)
}
value="Reset with values"
/>
I get en error: selectedBirthMonth is not defined.我收到错误: selectedBirthMonth 未定义。 Bassicaly, what I am aming for,on click, the whole component goes to default / a state it had when first rendered.
基本上,我的目的是,点击后,整个组件进入默认状态/首次呈现时的状态。 What am I missing?
我错过了什么?
reset didn't do the job somehow, but setValue did. reset 并没有以某种方式完成这项工作,但 setValue 做了。 However I am setting to a value, not to the original state after first render, but in my case it's same.
但是我设置的是一个值,而不是第一次渲染后的原始状态,但在我的情况下它是一样的。
<input
style={{ display: "block", marginTop: 20 }}
type="button"
onClick={() =>
etValue('selectedBirthMonth', lastAvaliableMonth)
}
value="Reset with values"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.