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.

Overriding DefaultValues if form is dirty

See original GitHub issue

Describe the bug When using register to provide DefaultValues to an input, it’s possible that the input component will be removed from the virtual DOM and eventually re-added. Upon re-rendering, register will initialize the input as the DefaultValue, even if the form state has been changed (dirty)

Expected behavior register could be able to use the most recent form state, instead of reverting to DefaultValues.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bluebill1049commented, Sep 18, 2019

@marracheco this is how we building wizzard form at work

const { register, handleSubmit, errors, setValue, watch } = useForm({ submitFocusError: false });

const onSubmit = step => data => {
    actions.updateCalculatorDataAction(data);
    actions.updateCalculatorStepsAction({
      [step]: true,
      currentStep: currentStep + 1,
    });
  };

const commonProps = { onSubmit, register, handleSubmit, calculatorData, errors, setValue, 
watch };

const section = () => {
    switch (currentStep) {
      case 0:
        return <BorrowingPowerCalculatorLoan {...commonProps} />;
      case 1:
        return <BorrowingPowerCalculatorIncome {...commonProps} />;
      case 2:
        return <BorrowingPowerCalculatorExpenses {...commonProps} />;
      case 3:
        return <CalculatorDemoPage4 {...commonProps} />;
      default:
        return <BorrowingPowerCalculatorLoan {...commonProps} />;
    }
  };

  return (
    <>
      {section()}
    </>
  );

Each page renders itself and during submission, it will update the data in the store.

0reactions
bluebill1049commented, Sep 19, 2019

@marracheco would you like to send a PR for your Wizzard example? we can have it under example folder. (keep it simple so devs can pick up the idea)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I override the defaultValues in useForm and maintain ...
I considered setValues but I want to use the isDirty function, which allows field validation and the value used to check whether the...
Read more >
Is there a way to track whether fields are dirty after the default ...
When the component is in "edit" mode I set the default values ... team data and then use reset() to overwrite the initial...
Read more >
Uncontrolled Components - React
In a controlled component, form data is handled by a React component. ... It can also be slightly less code if you want...
Read more >
useForm - FormState - Simple React forms validation
Make sure to provide all inputs' defaultValues at the useForm, so hook form can have a single source of truth to compare whether...
Read more >
React-admin - The Creation and Edition Pages - Marmelab
Alternatively, you can specify a defaultValue prop directly in <Input> components. React-admin will ignore these default values if the Form already defines ...
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