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.

`watch` is not returning correct values with `defaultValue` argument provided

See original GitHub issue

Describe the bug I have a checkbox-type component in my multi-step form, which uses reset(values) to restore the form state from higher-order state. Inside a checkbox component I use watch('name', false) to trigger some effects when checkbox is changed.

If I reset the form with values from state, where my value is true, watch still returns false. If I remove default value, then the value returned updates correctly, but this causes issues in my effects, so I still need this default value to be set.

I upgraded to v6.3.0, but it didn’t change anything

To Reproduce Steps to reproduce the behavior:

  1. Open the sandbox below
  2. Checkbox is checked by default. Notice that watch with default value returns false
  3. When you check/uncheck the checkbox, both values get synchronized
  4. If you then press the Reset button, you will see, that watch with default value returns wrong value again.

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-useform-template-gzmmu

Expected behavior watch return values should be in sync with and without the defaultValue, when the value is set in the form.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Redskinsjocommented, Nov 28, 2021

I have same error for defaultValues not returned by watch() on first render

0reactions
festusyumacommented, Aug 28, 2022

Please Has anyone been able to solve this. I see this in the docs for setting a default value with watch, but when I try to do this, the default value gets ovewritten with undefined

image image image

Read more comments on GitHub >

github_iconTop Results From Across the Web

useForm - watch - Simple React forms validation
When both defaultValue and defaultValues are supplied, defaultValue will be returned. This API will trigger re-render at the root of your app or...
Read more >
Set default values in React Hook Form - Stack Overflow
This is how I do, but the answer is not really correct. The values are set, but the defaultValues don't change this way...
Read more >
Watch out for mutable defaults in function arguments
You can set default values for function or method arguments, but think twice before you pick something that is mutable or can cause...
Read more >
API Documentation - Simple React forms validation
This will watch specified inputs and return their values. It is useful for determining what to render. When defaultValue is not defined, the...
Read more >
Solved: What is intended behaviour for default value of 'W...
The default value appears as the second argument of SELECTEDVALUE, and is returned if there is not exactly one value of YearMultiplier[YearMultipler] ...
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