react icon indicating copy to clipboard operation
react copied to clipboard

Calling onBlur and mutating state can cause onClick to not fire.

Open BJK opened this issue 11 years ago • 22 comments

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.

BJK avatar Jun 24 '15 14:06 BJK

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.

syranide avatar Jun 24 '15 15:06 syranide

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".

jimfb avatar Jun 24 '15 15:06 jimfb

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 avatar Dec 06 '18 17:12 OndeVai

@OndeVai OndeVai - This is a valid issue. I am also encountering this

chhikaradi1993 avatar Jul 15 '19 17:07 chhikaradi1993

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.

chhikaradi1993 avatar Jul 15 '19 17:07 chhikaradi1993

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).

ashkart avatar Aug 05 '19 15:08 ashkart

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

nguyenhoavuong avatar Sep 20 '19 03:09 nguyenhoavuong

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!

dashName avatar Dec 19 '19 20:12 dashName

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 avatar May 14 '20 05:05 anton-thushara

@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

johnneuland avatar Sep 02 '20 11:09 johnneuland

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.

benbakle avatar Oct 22 '20 02:10 benbakle

If there is a React bug here please file a new issue. We don’t track discussions on closed issues.

gaearon avatar Oct 22 '20 11:10 gaearon

If it helps anybody, this is how I solved it in my event handler

event.relatedTarget?.click()

charlesloubao avatar Jan 14 '21 16:01 charlesloubao

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.

brianpursley avatar Apr 30 '21 18:04 brianpursley

Using onMouseDown to replace onClick seems working to me.

JounQin avatar Sep 16 '21 10:09 JounQin

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)

leandro-ortiz-encora avatar Oct 04 '21 18:10 leandro-ortiz-encora

The onMouseDown worked partially for me. The click worked, but the onBlur stopped 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 ..

purushothaman-source avatar Oct 05 '21 10:10 purushothaman-source

Using onMouseDown to replace onClick seems working to me.

this shit works

LuisJoseLopezD avatar Feb 19 '22 01:02 LuisJoseLopezD

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

contigen avatar Apr 07 '23 11:04 contigen

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:

  1. Mousedown fires on button element
  2. Blur fires on input element. (now somehow the state is mutated, which cause changing position of elements on the screen)
  3. Mouseup fires on something which is not button anymore

=> Click won't fire

dung28-td avatar Sep 27 '23 04:09 dung28-td