-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Ensure that validated values object is same object that is saved to state (+ Reduce number of actions) #2732
Conversation
…tate; Reduce number of actions
@johnrom i think this is fine? |
I haven't tested, but based on a code review this looks like a nice performance enhancement. the only downside is that validations can now modify the values object. but so can any other part of the code, so I don't think it matters too much. |
This is true, but it's worth noting that validation was already able to modify (mutate) any child-objects/arrays of |
This pull request is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days |
This pull request is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days |
@MaximSagan is attempting to deploy a commit to the Formium Team on Vercel. A member of the Team first needs to authorize it. |
This pull request is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days |
If I understand correctly, this isn't actually the same because the This will be possible in #3089 because we provide getState() for exactly this purpose. @MaximSagan I think you should rebase this off of that branch and it can be merged once that is merged. |
This pull request is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days |
The problem
The main problem I set out to solve with this PR was the creation of two different
values
objects on everysetFieldValue
call.Namely, https://github.com/formium/formik/blob/c583eccc10378cbfc476c4cff3077e65b8e847ef/packages/formik/src/Formik.tsx#L79 and
https://github.com/formium/formik/blob/c583eccc10378cbfc476c4cff3077e65b8e847ef/packages/formik/src/Formik.tsx#L597
The former is used to update the
state.values
property, and the latter is used to validate the form (and ultimately update thestate.errors
object). The problem comes when the user wants to perform any kind of memoization of complex validation results, thevalues
object that gets validated initially onsetFieldValue
is lost (a different object is saved to state) so it cannot be used for comparison to determine whether to re-run validation.What I've done
To solve the problem stated above, on
setFieldValue
, I create a single newvalues
object, and this object is used to both update state and in validation. With this change, the distinction between theSET_FIELD_VALUE
action and theSET_VALUES
action was not needed, so I removed theSET_FIELD_VALUE
action.For consistency, I also removed
SET_FIELD_TOUCHED
andSET_FIELD_ERROR
actions.