`useWatch` method does not watch form values when `defaultValue` has a value.
See original GitHub issueDescribe 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
V6
- useForm Template
- useFieldArray Template
- Controller Template
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:
- Created 2 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top 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 >
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

Going to close this issue, for now, I will keep an eye when you have the PR. Thank you @YuitoSato for looking into this. 🙏
Yes, I will! I’ll give it a try for a couple of weeks.