v7: `{valueAsNumber: true}` inconsistent
See original GitHub issueDescribe the bug
Hi again! (Sorry for all the issues this week!)
There seem to be multiple things going on:
useController({rules: {valueAsNumber: true}})
doesn’t work. (It works withform.register
though.)useForm({defaultValues: {field: 0}}
doesn’t get used onvalueAsNumber
fields.- With
useController()
valueAsNumber fields, you have to force a value, or you get a react controlled/uncontrolled warning.
Codesandbox link (Required)
https://codesandbox.io/s/valueasnumber-bugs-ok2oz?file=/src/index.js
To see issues
- 3, change the value of the second slider. You’ll get the error.
- To see issue 2, just look at the default values on all of the sliders, compared to the
defaultValues
. - To see issue 1, change the values of all three sliders, then press
Submit
and look at the console.
Expected behavior
defaultValues
get used onvalueAsNumber
fieldsuseController({rules: {valueAsNumber: true}})
handles values exactly asform.register
does.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
fix(deps): update dependency react-hook-form to v7.41.0 by ...
This PR contains the following updates: Package Change Age Adoption Passing Confidence react-hook-form (source) 7.32.2 -> 7.41.0 Release ...
Read more >Migrate From V6 to V7 - Simple React forms validation
V6 to V7. Hi there,. React Hook Form focus on the following aspect on Version 7: ... <input ref={register({ valueAsNumber: true, validate: (value) ......
Read more >react-hook-form - Awesome JS
fix #9571 validation issue with unregister input with valueAsNumber (#9572) ... resetOptions: { keepDirtyValues; true, // only non dirty input will receive ...
Read more >Frequently Asked Questions — Snakemake 7.3.5 documentation
The best solution is to have a dictionary that translates a sample id to the inconsistently named files and use a function (see...
Read more >How to input only number in react-hook-form - Stack Overflow
Check the docs (V7): https://react-hook-form.com/api/useform/register <input type="number" {...register("test", { valueAsNumber: true, })} ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for your feedback. I will put that into consideration.
Ah, thanks. A few thoughts:
defaultValues
. Yikes!getControllerValue
logic for no reason?rules
param is the same as for register. Additionally, the type on useControllerrules
doesn’t actually block any of the value-related props. Screenshot below of TS with no errors…Use case:
My primary issue here is that I’m using a custom Field component (that gives a label, hover tip, shows errors, changes classNames, etc). Some inputs wrapped by Field are third-party (Slate, color picker, etc), but some are vanilla inputs. It makes the most sense for me to use
useController
for all of them. It limits the props I have to support, and I can avoid doing anything wonky to pass theform.register().ref
through. (No renames or anything.)I’m actually surprised the code for useController and register diverges so much. I expected useController to only add the extra outputs (fieldState, formState) for convenience.
Anyway, I’m interested to hear what you think.