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

First party asynchronous default values api #2335

Closed
benswinburne opened this issue Jul 23, 2020 · 2 comments
Closed

First party asynchronous default values api #2335

benswinburne opened this issue Jul 23, 2020 · 2 comments
Labels
feature request request a feature to be added

Comments

@benswinburne
Copy link

Is your feature request related to a problem? Please describe.

Asynchronous defaults are commonplace as they're often retrieved from storage when a UI is rendering

My use case is such that the UI presents some default values on checkboxes and radio buttons. If the user has never saved them before it doesn't matter, the consumer of these values has the same defaults elsewhere. If the users changes any of these values, the non default values are stored when changed. When the form is retrieved, defaults are set and the changed values are retrieved and set.

The reason the default value in this example is "flash of default" is because it the default value briefly flashes into the field while the asynchronous value is retrieved.

Describe the solution you'd like

Some sort of asynchronous defaults/suspense compatible api to set default values would be incredibly useful I think. Ideally one which is aware that it is setting defaults so it doesn't trigger changes on the form.

Describe alternatives you've considered

Obviously this is why I was trying to use to try and prevent the rendering of the field until the remote data was ready so the user didn't see the default value, then the stored value.

I wonder if perhaps a function for the defaultValues config or defaultValue prop could be async and provide a means by which to tell react-hook-form that the values are async.

const { register, handleSubmit } = useForm({
  defaultValues: async () => ({field1: 'bar'}),
});
<input
  name="firstName"
  type="text"
  ref={register}
  defaultValue={async () => {
    return 'string'
  }}
/>

Additional context

The other problem with using setValue or reset to update the default values after the form has first rendered is that it causes watch() and useWatch() to trigger (as the fields have rightly changed), but they're actually technically the defaults. This has caused me issues with autosaving on change etc as I render defaults, get saved values and put them into the fields, watch notices it changes and it tries to save again so lots of refs and value comparisons etc has to happen around that to stop it trying to save the values it just retrieved!

Related #2333

@bluebill1049
Copy link
Member

Thanks for the detailed feature request, I don't think we will implement such API for now, i will add a feature request and waiting for upvote for this issue.

@bluebill1049 bluebill1049 added feature request request a feature to be added waiting-up-vote Waiting for votes from the community. labels Jul 23, 2020
@bluebill1049 bluebill1049 added this to Pending Feature Requests in React Hook Form Jul 23, 2020
@bluebill1049
Copy link
Member

RFC: #2492

@bluebill1049 bluebill1049 moved this from Pending Feature Requests to Pause in React Hook Form Jun 29, 2021
@bluebill1049 bluebill1049 removed the waiting-up-vote Waiting for votes from the community. label Jul 8, 2021
@bluebill1049 bluebill1049 moved this from Pause to In progress in React Hook Form Sep 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2022
@bluebill1049 bluebill1049 moved this from In progress to Done in React Hook Form Dec 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request request a feature to be added
Projects
Development

No branches or pull requests

2 participants