Issue using DefaultValues
See original GitHub issueDescribe the bug I want to pre-populate a form using data from my redux store. Previously, if I refreshed the form page, the default values would be injected correctly.
Since upgrading to RHF v4.0+, If I refresh the form page, default values are not injected into inputs. But if I click out and back into the form page, the values are there.
Here’s a basic code example:
PersonalDetails.tsx
const PersonalDetails = () => {
const storedData = useSelector(
(state: IStoreState) => state.personalDetails,
);
const methods = useForm({
defaultValues: {
personalDetails: storedData,
},
});
const { handleSubmit } = methods;
if (storedData === undefined) {
return <SkeletonLoading />
} else {
return (
<FormContext {...methods}>
<form onSubmit={handleSubmit(onSubmit)}>
<InputString
name={personalDetails.firstName}
label="First name"
required={true}
/>
<InputString
name={personalDetails.lastName}
label="Last name"
required={true}
/>
<button type="submit">Save</button>
</form>
</FormContext>
}
InputString.tsx
const InputString = ( label, name, required, infoText ) => {
const methods = useFormContext();
return (
<div>
<Input
name={name}
type="string"
register={methods.register({ required })}
value={methods.watch(name)}
onChange={event => methods.setValue(name, event.target.value)}
/>
</div>
);
};
Hopefully, this is enough information to understand why this is happening but let me know if you need more context.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Default Values for 'Create Issue' screen - Atlassian Marketplace
Create issues faster and improve their quality with default values for "Create ... Open "Create Issue" screen and watch default values added automatically....
Read more >Create issues with preset default values
Create issues with preset default values · 6 - On each line, click on the "..." button and select the Set static value...
Read more >Default Values for 'Create Issue' screen - Confluence - Atlassian
Create issues faster and improve their quality with default values for "Create Issue" screen. Default Values for JIRA allows to automatically inject ...
Read more >Are there hidden problems with default values in software?
Virtually all information systems have "default values." We put them in our systems to make things easier for the system and the end-users....
Read more >Issue with default values, how to fix - Odoo
Issue with default values, how to fix ? ... Thanks for your subscription! ... It seems work fine, but in all open erp...
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

I do agree
reset({ defaultValues })is counter intuitive, here is the reason why and when you should use it.defaultValueworks well when your data is ready on the initial render, however sometimes defaultValue may come from API or delay, and that’s when you should usereset, i understand it would be better to use an API to calledsetValues, but i didn’t want to grow the amount API that user will have to remember and learn. That’s the reason i have decided to reusereset. Form RHF v1 till today, we haven’t changed any of the primary API and try to keep it simple for most of our users.hope above make sense.