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.

defaultValue passed to `watch` overrides defaultValues passed to `useForm` at first render

See original GitHub issue

watch method picks up a default value provided to it instead of picking up the values set in useForm and seems like this happens only at initial render. This makes it problematic to work with dynamic fields for me.

Here is a Sandbox example: https://codesandbox.io/s/dynamic-custom-fields-f5w03 Note that initially both animals have Cat type despite the defaultValue for the second animal was set to Dog in useForm. As soon as you click ‘Add animal’ button you’ll see that it gets fixed.

Expected behavior I’d expect that defaultValue in watch method will be used only in cases when it wasn’t set in useForm method.

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
bluebill1049commented, Nov 26, 2019
   const [inputValue, setInputValue] = React.useState(
      isCheckbox
        ? defaultChecked === undefined
          ? false
          : defaultChecked
        : defaultValue === undefined
        ? ''
        : defaultValue,
    );

we do check in react hook form input

1reaction
GProstcommented, Nov 26, 2019

@bluebill1049 seems like this works smoothly, thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

useForm | React Hook Form - Simple React forms validation
The defaultValues prop is used to populate the entire form values. It supports both sync and async set form default values. You can...
Read more >
DefaultValues from useForm is not updated correctly with ...
Setting defaultValue via useForm will only set the form values on the initial render. It behaves similar to useState , which will also...
Read more >
Create Dynamic Forms in React Using React Hook Forms
React Hook Form minimizes the number of re-renders, minimizes validate ... It will override the defaultValue passed in the useForm hook.
Read more >
Final Form Docs – `FieldProps`
A render function that is given FieldRenderProps , as well as any non-API props passed into the <Field/> component. For example, if you...
Read more >
React Query and Forms | TkDodo's blog
I have gone on the record already about how I am not a fan of copying ... and because our data is undefined...
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