question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v7: `{valueAsNumber: true}` inconsistent

See original GitHub issue

Describe the bug

Hi again! (Sorry for all the issues this week!)

There seem to be multiple things going on:

  1. useController({rules: {valueAsNumber: true}}) doesn’t work. (It works with form.register though.)
  2. useForm({defaultValues: {field: 0}} doesn’t get used on valueAsNumber fields.
  3. 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 on valueAsNumber fields
  • useController({rules: {valueAsNumber: true}}) handles values exactly as form.register does.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bluebill1049commented, Apr 6, 2021

Thanks for your feedback. I will put that into consideration.

1reaction
mikestopcontinuescommented, Apr 6, 2021

Ah, thanks. A few thoughts:

  1. My mistake with the defaultValues. Yikes!
  2. I think it makes more sense to keep register and useController as aligned as possible, for developer experience. Why have users reimplement getControllerValue logic for no reason?
  3. The performance of value-related rules is better than wrapping onChange/onBlur.
  4. The docs for useController indicate the rules param is the same as for register. Additionally, the type on useController rules doesn’t actually block any of the value-related props. Screenshot below of TS with no errors…
Screen Shot 2021-04-06 at 10 46 20 AM

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 the form.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found