New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue: error when using useWatch
with a name array and default values as object
#9530
Comments
thanks a lot will try to get it fixed today. |
This is actually not a bug, the return type of the useWatch is actually function FirstNameWatched({ control }: { control: Control<FormValues> }) {
const [a,b] = useWatch({
control,
name: ['firstName', 'lastName'],
defaultValue: { firstName: '', lastName: ''},
});
return (
<p>
Watch: {a} {b}
</p>
);
} |
So it's a typing error? |
I tested it on my end and it's fine (the screenshot above). |
Yes the type is fine but it produces a runtime error as reproduced here: https://stackblitz.com/edit/react-ts-beok4h?file=App.tsx |
seems like a bug to me, once you will confirm that it is a bug please let me know if I can take this issue! @bluebill1049 |
Oops, my bad i was focus on the type... |
Yes, please. Let me know if you need some assit. |
thanks, @bluebill1049, Can you please assign this to me on Github, just to be sure that no one else starts working on the same? |
Thank you very much for the help @neerajkrbansal1996 in advance. |
@bluebill1049 @neerajkrbansal1996 From my understanding it comes form this: react-hook-form/src/useWatch.ts Lines 183 to 187 in c21210b
When I think it should be something like this: const [value, updateValue] = React.useState<unknown>(
isUndefined(defaultValue)
? control._getWatch(name as InternalFieldName)
- : defaultValue,
+ : control._getWatch(name as InternalFieldName, defaultValue)
); Let me know if that helps and if I can do more to close this. |
thanks a lot, @adesurirey that's correct! |
thanks, @neerajkrbansal1996 for the willingness to fix the issue, if you are interested I can ping you with other issues in the near future thanks a lot @adesurirey for the report and also fixing the issue. |
thanks @bluebill1049 , let me know if can be of some help with other issues! |
Version Number
v7.40.0
Codesandbox/Expo snack
https://stackblitz.com/edit/react-ts-beok4h?file=App.tsx
Steps to reproduce
useWatch
so it uses an array asname
, as mentioned in the comment next to itdefaultValues
to an object following TS directivesNo errors when passing an array as defaultValues but then it generates other TS errors.
Expected behaviour
Should not crash, or type should be updated
What browsers are you seeing the problem on?
No response
Relevant log output
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: