Skip to content
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

Closed
1 task done
adesurirey opened this issue Dec 5, 2022 · 14 comments · Fixed by #9555
Closed
1 task done

issue: error when using useWatch with a name array and default values as object #9530

adesurirey opened this issue Dec 5, 2022 · 14 comments · Fixed by #9555
Labels
bug Something isn't working

Comments

@adesurirey
Copy link
Contributor

adesurirey commented Dec 5, 2022

Version Number

v7.40.0

Codesandbox/Expo snack

https://stackblitz.com/edit/react-ts-beok4h?file=App.tsx

Steps to reproduce

  1. Modify the TS doc example for useWatch so it uses an array as name, as mentioned in the comment next to it
  2. Update defaultValues to an object following TS directives
function FirstNameWatched({ control }: { control: Control<FormInputs> }) {
  const [firstName, lastName] = useWatch({
    control,
    name: ['firstName', 'lastName'],
    defaultValue: { firstName: 'default', lastName: 'default' },
  });

  return (
    <p>
      Watch: {firstName} {lastName}
    </p>
  );
}
  1. See the runtime error
object is not iterable (cannot read property Symbol(Symbol.iterator))

No 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

  • I agree to follow this project's Code of Conduct
@bluebill1049 bluebill1049 added status: under investigation aware of this issue and pending for investigation bug Something isn't working and removed status: under investigation aware of this issue and pending for investigation labels Dec 5, 2022
@bluebill1049
Copy link
Member

thanks a lot will try to get it fixed today.

@bluebill1049 bluebill1049 added question Further information is requested bug Something isn't working TS Typescript related issues and removed bug Something isn't working question Further information is requested labels Dec 5, 2022
@bluebill1049
Copy link
Member

This is actually not a bug, the return type of the useWatch is actually array instead of object.

function FirstNameWatched({ control }: { control: Control<FormValues> }) {
  const [a,b] = useWatch({
    control,
    name: ['firstName', 'lastName'],
    defaultValue: { firstName: '', lastName: ''},
  });

  return (
    <p>
      Watch: {a} {b}
    </p>
  );
}

Screenshot 2022-12-06 at 10 00 33 am

@adesurirey
Copy link
Contributor Author

So it's a typing error?

@bluebill1049
Copy link
Member

I tested it on my end and it's fine (the screenshot above).

@adesurirey
Copy link
Contributor Author

adesurirey commented Dec 6, 2022

Yes the type is fine but it produces a runtime error as reproduced here: https://stackblitz.com/edit/react-ts-beok4h?file=App.tsx

image

@neerajkrbansal1996
Copy link

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

@bluebill1049
Copy link
Member

So it's a typing error?

Oops, my bad i was focus on the type...

@bluebill1049
Copy link
Member

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

Yes, please. Let me know if you need some assit.

@bluebill1049 bluebill1049 added bug Something isn't working and removed TS Typescript related issues labels Dec 6, 2022
@neerajkrbansal1996
Copy link

neerajkrbansal1996 commented Dec 6, 2022

thanks, @bluebill1049, Can you please assign this to me on Github, just to be sure that no one else starts working on the same?

image

@bluebill1049
Copy link
Member

Thank you very much for the help @neerajkrbansal1996 in advance.

@adesurirey
Copy link
Contributor Author

adesurirey commented Dec 8, 2022

@bluebill1049 @neerajkrbansal1996 From my understanding it comes form this:

const [value, updateValue] = React.useState<unknown>(
isUndefined(defaultValue)
? control._getWatch(name as InternalFieldName)
: defaultValue,
);

When defaultValue is an object it is set as value and returned without any transformation. So then when deconstructing the result as if it was an array, the error is thrown.

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.

@bluebill1049
Copy link
Member

thanks a lot, @adesurirey that's correct!

@bluebill1049
Copy link
Member

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.

@neerajkrbansal1996
Copy link

thanks @bluebill1049 , let me know if can be of some help with other issues!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants