Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dirty flag when calling resetForm with current values #1986

Merged

Conversation

Tigge
Copy link
Contributor

@Tigge Tigge commented Nov 1, 2019

When you are resetting the form using resetForm and specify current
formik values as the next state the dirty hook won't be run since the
current values didn't change. This adds intialValues.current as a
dependency so the hook will run again if either of these changes.

This fixes the dirty part of issue #1976.

When you are resetting the form using `resetForm` and specify current
formik values as the next state the dirty hook won't be run since the
current values didn't change. This adds `intialValues.current` as a
dependency so the hook will run again if either of these changes.

This fixes the dirty part of issue jaredpalmer#1976.
@ianks
Copy link

ianks commented Nov 22, 2019

@jaredpalmer any chance of getting this merged?

src/Formik.tsx Show resolved Hide resolved
@Tigge Tigge requested a review from jaredpalmer November 27, 2019 21:46
@jaredpalmer jaredpalmer merged commit ec1d679 into jaredpalmer:master Dec 6, 2019
@Tigge Tigge deleted the resetForm-dirty-same-values-fix branch December 6, 2019 20:18
@babalugats76
Copy link

Thanks to you guys for all the awesome work. I hate to be that guy, but is there an ETA on this? For my projects, this is core functionality. Currently, I have no way to set dirty back to false unless I push the data down and rely upon form re-initialization via new props. This may be a "small" feature, but it is critically important. Many thanks!

@Tigge
Copy link
Contributor Author

Tigge commented Feb 27, 2020

@babalugats76 This has already been merged and was released in v2.0.7.

@babalugats76
Copy link

babalugats76 commented Feb 27, 2020

Thanks @Tigge! Looks like I need to do some more testing. I am using 2.1.4. I also have nested objects which I know have been problematic in the past--don't know if deep comparisons are supported since hooks are now prominently featured in Formik. I think I will will roll back to 2.0.7, recreate my use case, see what happens, and then roll forward from there.

Update
OK....I think I figured out my problem. Hope it helps others.

I pretty much had to set enableReinitialize to false. Also, how you specify the values to resetForm caused me some fits.

For me, I have a situation in which I start with a blank form--without a back-end database record. Originally, I was using enableReinitialize to true. On the first save, I would call a function that caused an effect to fire, fetching the newly created database record from the backend. This would push down the unique id of the record, i.e., props would change forcing the form to reinitialize. That worked great.

On subsequent saves, however, I want to simply use resetForm and provide the new values. This wasn't working when enableReinitialize was set to true. So, I tried a bunch of stuff, even conditionally setting enableReinitialize.

This is what I ended up with (inside my onSubmit):

if (matchId) { // If record exists
  await resetForm({ values }); // sets dirty false
} else { // otherwise
  const newValues = { ...values, matchId: results.data.matchId }; // augment with db id
  await resetForm({ values: newValues }); // sets dirty false
}

In the above record, matchId is the unique key from the database.

Basically, I just converted everything over to resetForm and nixed the code that required the form re-initialization. It would appear as if resetForm and enableReinitialization = true are mutually exclusive; either that, or I simply don't understand how things work :)

If I am barking up the wrong tree, definitely let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants