question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue using DefaultValues

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

26reactions
bluebill1049commented, Mar 4, 2020
Screen Shot 2020-03-05 at 8 23 22 am if `defaultValues` is not read on the initial load, you will need to use `reset({ defaultValues })` at useEffect. thanks for the detailed issue report.
11reactions
bluebill1049commented, Mar 29, 2020

Is there a particular reason the defaultValues is not read on the initial load?

I am asking this questions to learn and understand API design decisions being mades in this package. IMO, It seems counter intuitive to use reset({ defaultValues }) to set initial field values.

I do agree reset({ defaultValues }) is counter intuitive, here is the reason why and when you should use it.

defaultValue works 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 use reset, i understand it would be better to use an API to called setValues, 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 reuse reset. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found