Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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: form.reset(values) ignores undefined values #11751

Closed
1 task done
andykog opened this issue Apr 4, 2024 · 2 comments
Closed
1 task done

issue: form.reset(values) ignores undefined values #11751

andykog opened this issue Apr 4, 2024 · 2 comments
Labels
question Further information is requested

Comments

@andykog
Copy link

andykog commented Apr 4, 2024

Version Number

7.51.2

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/react-hook-form-reset-undefined-w3p8ks

Steps to reproduce

Initialize the form with some values or defaultValues:

const userProfileForm = useForm({
  values: { revision: 5, firstName: 'a', image: {url: 'https://image.png'} },
})

Assume that the data was re-fetched from server and the form needs to be reset to reflect the actual state (in this scenario the profile image was removed on another device):

userProfileForm.reset({revision: 6, firstName: 'a' }); // <- image was removed

Result: the image value wasn't reset.

Additional info: it used to work fine in version 7.21.0

Workaround 1: replace all undefined values with null when calling reset:

userProfileForm.reset(_.defaultsDeep(newUserProfile, {image: null}))

Workaround 2: call reset with and without arguments multiple times (you can try to press buttons in the pvovided codesandbox a few times, eventually it will reset lastname), this behaviour is inconsistent.

Discussion

Expected behaviour

It should reset the form state completely to match the provided object without “merging” it with the previous values.

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049
Copy link
Member

bluebill1049 commented Apr 4, 2024

we don't support undefined value, you can find similar rule for other hook such as useController defaultValue or defaultValues

@bluebill1049 bluebill1049 added the question Further information is requested label Apr 4, 2024
@andykog
Copy link
Author

andykog commented Apr 4, 2024

@bluebill1049 Would be useful if there was a way to reset form state completely. Also I think reset behaviour should be consistent (see my comment about workaround 2) it shouldn't behave differently depending on how many time it was invoked.

@react-hook-form react-hook-form locked and limited conversation to collaborators Apr 4, 2024
@bluebill1049 bluebill1049 converted this issue into discussion #11752 Apr 4, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants