formik icon indicating copy to clipboard operation
formik copied to clipboard

Include FormikState in onReset/onSubmit callback

Open jonaswitt opened this issue 4 years ago • 10 comments

Resolves #2709

Let me know if anything else should be changed, I tried to update the docs as best as possible.

jonaswitt avatar Aug 18 '20 08:08 jonaswitt

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

github-actions[bot] avatar Nov 20 '20 00:11 github-actions[bot]

@jonaswitt is attempting to deploy a commit to the Formium Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Apr 12 '21 15:04 vercel[bot]

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

github-actions[bot] avatar Jun 19 '21 00:06 github-actions[bot]

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

github-actions[bot] avatar Jul 22 '21 00:07 github-actions[bot]

This is also somewhat related to #2365 which adds access to the initialValues in the submit handler.

amaechler avatar Sep 20 '21 22:09 amaechler

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?

jonaswitt avatar Sep 21 '21 07:09 jonaswitt

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.

johnrom avatar Sep 21 '21 15:09 johnrom

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.

johnrom avatar Sep 21 '21 15:09 johnrom

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)

vercel[bot] avatar May 11 '22 07:05 vercel[bot]

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.

jonaswitt avatar May 11 '22 07:05 jonaswitt