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.

Setting default value

See original GitHub issue

Describe the bug When taking the form methods by using useForm<Type>({ defaultValues: values }) , I cannot get values by using getValues()

To Reproduce Steps to reproduce the behavior:

  1. Get form methods from useForm<Type>({ defaultValues: values })
  2. Use <FormProvider {...formMethods}>{children}</FormProvider>
  3. Get values from useFormContext inside a nested component
  4. And the values will be {}

Codesandbox link (Required) https://codesandbox.io/s/brave-wescoff-s8i8k

Expected behavior getValues() method had to return default values as I had provided them

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Browser: Chrome v84.0.4147.89

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:39 (22 by maintainers)

github_iconTop GitHub Comments

15reactions
bluebill1049commented, Jul 22, 2020

looks like everyone is expected getValues to return defaultValues, we will sort it out then.

5reactions
benswinburnecommented, Jul 29, 2020

In case it is useful, I’ve been getting around this by merging in the defaults with the current value by accessing the ref via control. It’s not necessarily conventional but it works.

const formDefaults = form.control.defaultValuesRef.current;
const fieldValues = getValues(); // this includes defaultValue props on fields

const values = {
  ...formDefaults,
  ...fieldValues,
};

Obviously I’m just merging single depths here as I don’t use any dot notation but you could just as easily use values = deepmerge(...) for example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL DEFAULT Constraint - W3Schools
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new records,...
Read more >
Set a Default Value - CustomGuide
In Design View, click the field you want to add a default value to. · Click the Default Value box. · Enter the...
Read more >
3 Ways to Set Default Value in JavaScript | SamanthaMing.com
Let's break down the 3 different ways to set Default Values using logical operator, ternary, and if/else...
Read more >
Set a Field's Default Value in an MS Access Table –
A default value is automatically entered in a field for a new record. You can set a default value for table fields set...
Read more >
SQL Default Value: How To with Examples - Database Star
Setting a default value in SQL can be done when you create the table or on an existing table's column. The default value...
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