admin管理员组文章数量:1420187
I'd need to add custom handler in the onChange event of a checkbox, however, doing so onChange won't reflect to react-hook-form.
I've followed the instructions how to do custom onChange
by following the chapter
Custom onChange, onBlur: /
However cannot make it work.
Seeing in the DevTool the first checkbox change reflects to form just fine however the latter with a custom handler does not.
...
const subscribeToNewsLetter = register('subscribeToNewsLetter')
...
<div>
<label> I consent </label>
<input
type="checkbox"
{...register('consent')} //This checkbox without any custom handler works fine
/>
</div>
<div>
<label>Subscribe to newsletter</label>
<input
type="checkbox"
onChange={(e) => {
subscribeToNewsLetter.onChange(e); // This needs to do custom stuff in onChange
handleChange(e); // However doing so
}} // the change is not registered to hook-form
onBlur={subscibeToNewLetter.onBlur}
ref={subscibeToNewLetter.ref}
/>
</div>
...
I've been fiddling it here which illustrates the problem the best.
I'd need to add custom handler in the onChange event of a checkbox, however, doing so onChange won't reflect to react-hook-form.
I've followed the instructions how to do custom onChange
by following the chapter
Custom onChange, onBlur: https://react-hook-form./api/useform/register/
However cannot make it work.
Seeing in the DevTool the first checkbox change reflects to form just fine however the latter with a custom handler does not.
...
const subscribeToNewsLetter = register('subscribeToNewsLetter')
...
<div>
<label> I consent </label>
<input
type="checkbox"
{...register('consent')} //This checkbox without any custom handler works fine
/>
</div>
<div>
<label>Subscribe to newsletter</label>
<input
type="checkbox"
onChange={(e) => {
subscribeToNewsLetter.onChange(e); // This needs to do custom stuff in onChange
handleChange(e); // However doing so
}} // the change is not registered to hook-form
onBlur={subscibeToNewLetter.onBlur}
ref={subscibeToNewLetter.ref}
/>
</div>
...
I've been fiddling it here which illustrates the problem the best. https://stackblitz./edit/react-hook-form-fiddling-zlnhzs
Share Improve this question asked Aug 5, 2021 at 6:06 anmatikaanmatika 1,7216 gold badges23 silver badges30 bronze badges1 Answer
Reset to default 3You've to update the input like that:
<input
type="checkbox"
{...subscibeToNewLetter}
onChange={e => {
subscibeToNewLetter.onChange(e);
handleChange(e);
}}
/>
版权声明:本文标题:javascript - React-hook-form custom onChange handler in checkbox -> change is not registered - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745324672a2653534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论