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.

`useWatch` method does not watch form values when `defaultValue` has a value.

See original GitHub issue

Describe the bug useWatch method does not watch form values when the defaultValue has a value. When defaultValue is undefined, form values are watched.

To Reproduce The expected value A is not displayed even though defaultValues has a value in the codesandbox page below.

import "./styles.css";
import { useForm, useWatch } from "react-hook-form";

export default function App() {
  const { control } = useForm({
    shouldUnregister: false,
    defaultValues: {
      select: "A"
    }
  });

  return (
    <span>
      <SelectedValue control={control} />
    </span>
  );
}

const SelectedValue = function SelectedValue({ control }) {
  const selectedValue = useWatch({
    control,
    name: "select",
    defaultValue: ""
  });

  // Value A is not displayed.
  return <>selectedValue: {selectedValue}</>;
};

Codesandbox link (Required) https://codesandbox.io/s/immutable-browser-09fxk?file=/src/App.js

V7

  • JS Template
  • TS Template

V6

Expected behavior form values are watched by the useWatch method

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bluebill1049commented, Apr 22, 2021

Going to close this issue, for now, I will keep an eye when you have the PR. Thank you @YuitoSato for looking into this. 🙏

1reaction
YuitoSatocommented, Apr 20, 2021

I see what you mean @YuitoSato want to send a PR on this?

Yes, I will! I’ll give it a try for a couple of weeks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useWatch | React Hook Form - Simple React forms validation
The initial return value from useWatch will always return what's inside of defaultValue or defaultValues from useForm . The only difference between useWatch...
Read more >
React Hook Form useWatch not displaying correct value in ...
For anyone that may have this issue, I was able to fix it by using setValues to initially set the value on render...
Read more >
Form - Ant Design
Name (Watch to trigger rerender). Age (Not Watch). Name Value: Watch Hooks. useWatch helps watch the field change and only re-render for the...
Read more >
React-Hook-Form Library | Common Use Cases
We can use watch API. This method will watch specified inputs and return their values. It is useful to render input value and...
Read more >
API Documentation - Simple React forms validation
Note: Values defined in defaultValues will be injected into watch as defaultValue . Note: defaultValues doesn't auto populate with the manually registered input...
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