Formik: Anchor within (focused) form needs to be clicked twice

Created on 14 Feb 2019  路  5Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

If you place an anchor within a formik form, this anchor needs to be clicked twice if one of its inputs is focused.
Our use case is: you open a site with a form and the first input is (auto)focused so you can start filling in our form. At the bottom there a 2 actions: a submit button and a cancel link (which will simply redirect you to the previous page). If I open the site, scroll down and click "cancel" nothing happens besides form validating. I must hit the cancel link one more time to get redirected.

Expected behavior

I only want to click the cancel link once :)

Reproducible example

https://codesandbox.io/s/54lnrqylx4

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.5.0
| React | 16.8.1
| TypeScript | 3.3.1
| Browser | all
| npm/Yarn | 1.13
| Operating System | OSX Mojave

Most helpful comment

Yeah that came also to my mind. It seems that our component (and the basic anchor tag in it) gets re-rendered (and replaced) caused by the setState in the onBlur handlers. The browser loses the click event on the anchor and nothing happens.

Its fixable for us by using React.memo() on our component which will suppress an additional rerender (because the props dont change)

All 5 comments

@otbe the first click fire the onBlur event on your <a> component and the onBlur execute the form validation.
you can fix it by disabling the validateOnBlur={false} (a property of <Formik/> component), but this will disable also the live validation effect on your form.
i don't know if there is an option to disable validateOnBlur in a specific component inside <Formik/> ?!

Yeah that came also to my mind. It seems that our component (and the basic anchor tag in it) gets re-rendered (and replaced) caused by the setState in the onBlur handlers. The browser loses the click event on the anchor and nothing happens.

Its fixable for us by using React.memo() on our component which will suppress an additional rerender (because the props dont change)

I'm still experiencing this even with memo. I had to use onMouseDown on the link components that are within the scope of Formik because onBlur ended up rerendering the form and the link triggering the onClick. I think this is simply how React works, but it confused me for a time.

Edit: memo didn't fix it for me because I'm using the HOC and props do change after validation runs on blur.

@brandonarbini I had the same issue, and I could solve it only by using onMouseDown.
Did you find out any other reason for this behaviour. It's not that clear to me.

I left a pretty good description of the issue here: https://github.com/jaredpalmer/formik/issues/1796#issuecomment-528078537

And a few options for solving it here: https://github.com/jaredpalmer/formik/issues/1796#issuecomment-605685583

tl;dr: The validation error message is pushing down your submit button before mouseup, resulting in the click hitting outside of the button.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredpalmer picture jaredpalmer  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments

green-pickle picture green-pickle  路  3Comments

emartini picture emartini  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments