formik
formik copied to clipboard
Include FormikState in onReset/onSubmit callback
Resolves #2709
Let me know if anything else should be changed, I tried to update the docs as best as possible.
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
@jonaswitt 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
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 is also somewhat related to #2365 which adds access to the initialValues
in the submit handler.
This is also somewhat related to #2365 which adds access to the
initialValues
in the submit handler.
Good point, thanks! I was not aware of that PR when I started this one. The change in #2365 would have probably been sufficient for me to reach my initial goal (although I was looking for the touched
data, having initialValues
would be just as good).
Now, who can get either of these PRs merged?
In v3 the Formik API will have a function called getState()
which will return the entireity of Formik's internal state and can be used in a callback like this.
#3231
onSubmit={(values, formik) => {
// note that `values` above have been validated
// whereas the values returned from `getState()` below may be future unvalidated values
const { errors, touched, initialValues, ...etc } = formik.getState();
if (etc.values === values) {
// user hasn't edited the form since submitting
}
}
Using the state where this PR is using it may be unsafe in some cases, as it will only be as current as the render which assigned the callback to onSubmit() -- though it might be an accurate snapshot of the values when the submitted values were validated.
Another thing to consider is we already have a request to add a prop to onSubmit for context, #2437, basically just any old information that needs to be sent, like click event or whatever.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Name | Status | Preview | Updated |
---|---|---|---|
formik-docs | ❌ Failed (Inspect) | May 11, 2022 at 7:13AM (UTC) |
Since this is a slightly different use case than v3's getState, I would support adding this functionality, however I wouldn't spread state on every render -- only when the callbacks are executed.
Thanks for your feedback on this PR and sorry I lost track of it for a little bit. I have pushed the suggested changes. Maybe it can still be helpful before Formik 3 ships.