-
-
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
onBlur validation cancels out onSubmit #786
Comments
Related React issues: These have been closed but reading through the comments there is no true resolution. But there has got to be a clean workaround. |
Looks like this could be a batching issue 🤔 React pre-16 only batches events, and |
Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal. |
ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it. |
@enagy-earnup any luck with this? |
@ivoreis yes! I was going about this all wrong, and it showed 😄 I was trying to put the submit on the button as opposed to putting it on the form, and that was causing a number of issues. I don't think my example really shows the code at fault. In short: use standard html best practices 😅 |
I fixed this issue by using |
Summary
When
onChange={handleChange}
is used in conjunction withonBlur={handleBlur}
, theonSubmit={handleSubmit}
event is not triggered until the second attempt. After some digging, I believe this is because thesetState(...)
calls inhandleBlur
are canceling out theonClick
oronSubmit
events. Having fiddled around with the internals, I've noted that without thesetState(...)
inside ofhandleBlur
, this does not happen.Current Behavior
I'm using the following code:
Steps to Reproduce
onBlur={handleBlur}
,onChange={handleChange}
, andonSubmit={handleSubmit}
onSubmit
callback will not have been firedonSubmit
callback will have been firedExpected behavior
My expectation is that, with the provided configuration, the
onSubmit
callback would be fired the first time around.Suggested solution(s)
I have had luck in resolving this issue by doing the following within
handleBlur
:Effectively, this forces the state change side-effects from
handleBlur
to be pushed back onto the event queue, thus allowing it to be prioritized behindhandleSubmit
. This definitely seems like a sketchy way to solve this problem, however.Additional context
CodeSandbox Link:
The text was updated successfully, but these errors were encountered: