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

isSubmitting is still true if synchronous onSubmit throws error #3135

Closed
mryanlo opened this issue Apr 2, 2021 · 3 comments
Closed

isSubmitting is still true if synchronous onSubmit throws error #3135

mryanlo opened this issue Apr 2, 2021 · 3 comments

Comments

@mryanlo
Copy link

mryanlo commented Apr 2, 2021

Bug report

Current Behavior

If onSubmit() is defined as a synchronous function, then any time an exception occurs within submission execution will cause the isSubmitting state to stay true.

Expected behavior

isSubmitting is set to false after an error occurs during onSubmit() execution.

Reproducible example

Code sandbox: https://codesandbox.io/s/formik-issubmitting-error-example-k0fwd

  1. In the sandbox I linked, fill out the email form, though not required
  2. click submit (i defined the onSubmit() function to throw an error rather than show alert, and it's synchronous)
  3. Observe the props in the json printout shows "isSubmitting": true and the submit and refresh buttons are still disabled. Changing the form doesn't reset them

Suggested solution(s)

See expected behavior section.

Additional context

This has proved to be a problem for me because I need to use some non-async calls for my form submission which use the .then() promise callbacks. Because my submission buttons disable if isSubmitting is true, they stay disabled.

Your environment

Software Version(s)
Formik 2.2.5
React 16.11.0
TypeScript 3.8.3
Browser n/a
npm/Yarn n/a
Operating System n/a
@mryanlo
Copy link
Author

mryanlo commented Apr 2, 2021

Just saw this remark on the onSubmit API.
image

I have changed my code to use async submissions to resolve this issue on my end, but wanted to ask if there is any future work to make synchronous submissions set isSubmitting to false after finishing? it might mean fewer conditionals users have to bake into their own code and perhaps a bit of a re-render performance boost.

https://formik.org/docs/api/formik#onsubmit-values-values-formikbag-formikbag--void--promiseany

@johnrom
Copy link
Collaborator

johnrom commented Apr 3, 2021

@mryanlo we don't want to assume that isSubmitting should be false after a sync callback because there is no way to tell whether the dev used asynchronous code in a fire-and-forget format. Examples are functions like setTImeout which don't return a Promise.

We should probably have forced users to wrap code like this in a Promise from the beginning and setSubmitting(false) automatically after sync code, but this didn't make too much sense when Promises were less ubiquitous and I don't think it makes sense to do this now, when there's probably so much code out there that works without this limitation.

@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2021

This issue 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 github-actions bot added the stale label May 4, 2021
@github-actions github-actions bot closed this as completed Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants