question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

click handler on submit button inconsistently called

See original GitHub issue

🐛 Bug report

Current Behavior

I have a form set up with:

  • initialValues prop on <Formik /> which populates all fields with valid values
  • Submit button of type submit
  • onSubmit prop on <Formik />
  • validateOnBlur set as default
  • validateOnChange set as default
  • validationSchema prop on <Formik /> which contains Yup validation schema
  • A click handler on the submit button. The purpose of this click handler is arbitrary. For purposes of this bug assume it simply console logs a message but it not used in any way to manipulate the form values nor interact in any way with the Formik API.

Under these conditions I have a form which is pre-populated with the initial values that pass the validation schema. I invalidate one of the fields. Then without triggering a field blur event I click directly on the submit button. The validation is triggered but the click handler on the submit button is called only some of the time suggesting a race condition.

Expected behavior

The click handler on the submit button is always called whenever the submit button is clicked.

Your environment

Software Version(s)
Formik 1.5.4
React 16.8.6
TypeScript N/A
Browser Chrome 76.0
npm/Yarn npm
Operating System Mac OSX 10.14.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

12reactions
martinpengellyphillipscommented, Mar 31, 2020

For reference, I fixed this in my case by attaching the following to my submit button in order to subvert the blur behaviour.

    onMouseDown={(event)=>{event.preventDefault()}}
1reaction
johnromcommented, Sep 4, 2019

Can you create a sandbox for this issue?

My guess at the moment is going to be that when you click the submit button, the error appears and pushes the submit button on your form down, so it actually moves past where you originally clicked, resulting in a click on the container instead of the submit button.

https://codesandbox.io/s/91q6rxmmrp

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form 'onsubmit' not getting called - Stack Overflow
When you call the form's submit function, the submit event is not fired ... In your example, I would remove the click handler...
Read more >
Handling Events - React
React events are named using camelCase, rather than lowercase. With JSX you pass a function as the event handler, rather than a string....
Read more >
.submit() | jQuery API Documentation
The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to...
Read more >
<input type="submit"> - HTML: HyperText Markup Language
When the click event occurs (typically because the user clicked the button), the user agent attempts to submit the form to the server....
Read more >
Powerapps change color of selected gallery item - Seba Online
Behind the OnSelect event of the button I use the following code to create a ... In that case, PowerApps provides a Button...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found