Calling onBlur and mutating state can cause onClick to not fire.
We encountered this while trying to have different parts of form validation happen on both the onBlur and the onClick. It seems that if you update State on an onBlur, and that state element is injected into the DOM via JSX, positionally ABOVE a button with an onClick--the onClick will not fire.
I believe this is related to this issue: https://github.com/facebook/react/issues/2291
See this http://jsfiddle.net/kb3gN/11477/ which is a variation on the example provided above. If you enter some text, you'll have to click the button twice, to see click: true.
Not 100% confident that order matters in the DOM, but you can see it consistently reproducable in the JSFiddle above.
@andela-domonori first noticed the weird behavior.
This is browser behavior and it seems entirely correct to me. The button receives the mouse down event but since the element moves immediately after then mouse up happens outside and no click is registered. Do the same thing but move your cursor back over the button first and then release, now you get the click as expected.
Confirmed, it looks like the issue is that you're not getting a click as a result of the button moving. If you put the error output under the button (such that it won't affect the positioning of the button), everything behaves as expected (so it's not an issue related to mutating state, it's an issue related to you moving things in the dom). Also, all the things @syranide pointed out. I'm going to close this as "not a react issue".
I don't think it has to do with the button moving. I did a similar test without the button moving and submit did not fire. It looks like if blur causes a re render, submit is not even fired.
@OndeVai OndeVai - This is a valid issue. I am also encountering this
For me there is no error in any state. My form is filled and valid. But I am doing field validation on blur event then my component is rerendering and onClick event is not firing.
I switch my "isEditState" flag on my "EditableField" component in onBlur for the input which fired when I click "Ok" button to save input text (And on clicking somewhere else I drop text changes in that onBlur callback). So the onClick event doesnt fire when 'Ok' clicked. I do not do any movements in a DOM tree.
Workaround:
const blurTextField = (e) => {
if (isEditState && isNodeClassContain(e.relatedTarget,'editButton')) {
setIsEditState(false);
}
};
useEffect(() => {
document.addEventListener('click', blurTextField);
// some stuff may be here
return () => {
document.removeEventListener('click', blurTextField);
};
}, [isEditMode]);
The point is not to use onBlur. And use click or focus event listener on document with detecting which element focused or clicked exactly (by class or id or whatever you want).
It seem like blur event follow by setState causes a rerender component. I have a simple solution to improve: using onMouseLeave event instead of using onBlur But it's not perfect solution because my input will lost typing when mouse leave the input
It seem like blur event causes a rerender component. I have a simple solution to improve: using onMouseLeave event instead of using onBlur But it's not perfect solution because my input will lost typing when mouse leave the input onMouseLeave={hideMe} ... ... hideMe = (e) => { const currentTarget = e.currentTarget; setTimeout(()=>{ if (!currentTarget.contains(document.activeElement)) { this.setState({ showSuggestions: false }) } }, 0); }; this works!
i experienced the same issue and found a workaround.
onBlur={() => setTimeout(onBlur, 500)}
override the onBlur and delay it a bit. basically delay what ever should happen after onBlur by about 500ms.
@anton-thushara @dashName @jimfb Could you look into problem please? A codesandbox is also given. https://stackoverflow.com/questions/63686646/stop-handleblur-from-doing-anything
The work around using setTimeout works because events are fired in order of priority. The event listener click comes after blur in priority. This causes a blur to fire but not the click. You can use mousedown instead of click due to it's priority over blur. By using setTimeout on the blur it delays the blur and allows for the click to take priority.
If there is a React bug here please file a new issue. We don’t track discussions on closed issues.
If it helps anybody, this is how I solved it in my event handler
event.relatedTarget?.click()
If it helps anybody, this is how I solved it in my event handler
event.relatedTarget?.click()
Almost.
This works for the scenario where you click on a button, but if you just tab to the next control, which happens to be a button (or in my case a hyperlink), it clicks it, which is not the tab behavior you want.
Using onMouseDown to replace onClick seems working to me.
In my case, some components appear/disapear based on a state controlled by the onBlur. So, the onClick of the other was not working because the scroll was being changed after the onBlur, when the new elements were added at the DOM.
The onMouseDown worked partially for me. The click worked, but the onBlur stopped being called.
My solution was to keep the onClick and add a setTimeout of 100ms inside the onBlur. So, the click can be executed before the onBlur mutates the state and cause all the consequences (rerendering + add the new elements at the DOM + change scroll)
The
onMouseDownworked partially for me. The click worked, but theonBlurstopped being called.
@lortiz-daitan same issue I faced recently in my case also there is appear/ disappear element s the onMouseDown will solve this issue ...this is event order onMouseDown -> onMouseUp -> onBlur -> onClick ...if you put event.preventDefault() inside onMouseDown function then onBlur won't fire ...
https://codepen.io/mudassir0909/pen/eIHqB
And also you have to be careful ,when using onMouseDown ,because it will trigger for left/right/mouse wheel click .. you can check which key is pressed using event.button if it returns 0 it means left click is happened if it is 1 it means wheel is clicked if it is 2 it means right click is happened ..
Using
onMouseDownto replaceonClickseems working to me.
this shit works
Calling the .click() method in a focus event's handler works for my use case; anyhow, is this a react thing, I'll know how to write my code comment better
Perhaps the element you are clicking is moved. According to MDN, click event fires after both the mousedown and mouseup events have fired
So I guess, the scenario would be:
- Mousedown fires on
buttonelement - Blur fires on
inputelement. (now somehow the state is mutated, which cause changing position of elements on the screen) - Mouseup fires on something which is not
buttonanymore
=> Click won't fire