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.

resetFieldState breaking hard on not-yet mounted fields

See original GitHub issue

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

Getting the following error when conditionally rendering and resetting a field (subcategory) based on another field (category)

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at getValidators (final-form.es.js:568)
    at runFieldLevelValidation (final-form.es.js:581)

What is the expected behavior?

// Resets the subcategory to undefined
form.change("subcategory", undefined);
// Resets the field state
form.resetFieldState("subcategory");

to work whether or not the field subcategory is mounted or not.

Sandbox Link

https://codesandbox.io/s/react-final-form-simple-example-7jt63

What’s your environment?

react 16.9.0 final-form 4.18.5 react-final-form 6.3.0

Other information

The expectations:

  • show subcategory field only after the user make a selection on category field
  • clearing any selections in subcategory field when the user change category
  • resetting the state of subcategory field (to hide any validation errors or messages)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:11

github_iconTop GitHub Comments

9reactions
santinocommented, May 4, 2020

+1 resetFieldState not working for me neither on a conditional field.

My use case is that when a “parent” field value changes I want to reset the state of the depending conditional fields like they had never been interacted with. In this case the conditional field might have never been or no longer be rendered; but it has been registered and so I would expect Final Form to imperatively perform operations on it.

This issue has been opened since Sept would be great to get a comment from maintainers so we know what we can expect.

5reactions
vitalylukyanenkocommented, Mar 5, 2020

resetFieldState still doesn’t work

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Final Form reset form fields and validation but retain ...
I managed to achieve this by looping through all form posted values and resetting each individually: const submitForm = async (values, ...
Read more >
mozilla-central: changeset 392810 ... - Mercurial
resetFieldStates (); + } + } + + // Unregister listeners once no field is in AUTO_FILLED state. + if (!this.hasFilledSection()) { +...
Read more >
Final Form Docs – `FormApi`
Returns the state of a specific field, of type FieldState , as it was last reported to its listeners, or undefined if the...
Read more >
useForm - reset - React Hook Form
Important: this keep option doesn't reflect form input values but only dirty fields form state. keepDirtyValues, boolean. DirtyFields and isDirty will remained, ...
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