You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
React onBlur events not firing during unmount #12363
If a DOM element rendered by a React component has focus, and the React component unmounts, the React onBlur event does not fire on parent DOM elements.
I would expect that, just like the browser fires a focusout event when removing a DOM node, React would fire an onBlur events up to parent nodes when the focused node is removed / unmounted.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React: 16.2
Mac OS X: 10.13.2
Browser: Chrome 67.0.3366.0, Chrome 64.0.3282.186
No idea if this worked in earlier versions of React.
Reacted by Tomasz Przysiwek, Erika Miguel, Arthur, Anthony Salani, Jari Pekkala, Bob Cook, Oleh, Alexandr Motuzov, Matej Beňo, Brian Pursley and 11 more
I suspect this is a result of how React handles events that occur during reconciliation. Currently, React disables the synthetic event system before committing. That means events that occur as a result of node removal aren't actually handled.
We've talked about having an event queue that gets processed after the commit phase, but right now there's no current plan to do that.
Sounds like rather than onBlur bubbling we'd want focusout support via onFocusOut since browsers don't bubble native blur events and doing so in React could cause bugs by breaking those expectations.focusout is not supported yet though for various reasons: #6410
Today I learned that React actually already bubbles blur events, unlike browsers, which makes me sad 😢Now I understand why this issue was created.
I have noticed similar behaviour with the onChange event. I'm not sure if it merits a separate issue, so I'm posting a response here first. I'm happy to move my comment to a new issue, if need be.
In this scenario, once you check one of the boxes, it's place in the virtual DOM changes. As a consequence of that, the change event gets lost while the click event bubbles up to the document. If I have understood @aweary correctly, this is currently the intended behaviour from the point of view of React, yes?
@jpkempf A separate issue is best for something that doesn't look like what's being described by this issue's title. Because otherwise it's unsearchable. Thanks.
Attaches the onBlur event handler directly to the underlying DOM element via a useEffect, requiring attaching a ref to the input itself
Uses a forwardRef combined with compose-react-refs to ensure we can still pass through a ref and also have access to a ref inside the wrapper
Uses a ref to track the previous event handler for cleanup (we couldn't rely on the useEffect clean up to do this because I believe that happens during the reconciliation, meaning the event handler is removed before being called in this use case - I have verified this)
There is one major caveat here though - because it requires attaching an event handler to the underlying element, it means that handler will be dealing with the raw event rather than React's SyntheticEvent, which may be a deal breaker for you.
To save you clicking the link, here's the code for the wrapper (which could equally have been written as a HOC):
I wanted to submit a form via onBlur and encountered this bug. In my case, I was conditionally rendering either the data to display or a form to edit that data in the same place.
Something like:
Not sure if obvious for everyone, but there is another workaround:
Don't render the elements conditionally, but apply the display: none CSS property to either one of them. That way, the elements won't get re-/mounted and hence the onBlur callback is working as expected:
// example uses TailwindfunctionPost({ isPostEditMode }: {isPostEditMode: boolean}): JSX.Element{return(<div><p>Post</p>{/* Form */}<formclassName={isPostEditMode ? 'hidden' : 'block'}onBlur={...}>
...
</form>{/* Data */}<divclassName={isPostEditMode ? 'block' : 'hidden'}>Data</div></div>)}
Here is a possible workaround. In my case I have a button that unmount component with input inside. So we can check an active element and trigger blur() before unmount.
Activity
aweary commentedon Mar 27, 2018
I suspect this is a result of how React handles events that occur during reconciliation. Currently, React disables the synthetic event system before committing. That means events that occur as a result of node removal aren't actually handled.
We've talked about having an event queue that gets processed after the commit phase, but right now there's no current plan to do that.
jayphelps commentedon Apr 16, 2018
Sounds like rather than onBlur bubbling we'd wantfocusoutsupport via onFocusOut since browsers don't bubble nativeblurevents and doing so in React could cause bugs by breaking those expectations.focusoutis not supported yet though for various reasons: #6410Today I learned that React actually already bubbles blur events, unlike browsers, which makes me sad 😢Now I understand why this issue was created.
jpkempf commentedon Apr 26, 2018
I have noticed similar behaviour with the
onChangeevent. I'm not sure if it merits a separate issue, so I'm posting a response here first. I'm happy to move my comment to a new issue, if need be.Here's a reduced test case (open the console and click around a bit): https://codesandbox.io/s/84wz9wlvxl
In this scenario, once you check one of the boxes, it's place in the virtual DOM changes. As a consequence of that, the
changeevent gets lost while theclickevent bubbles up to thedocument. If I have understood @aweary correctly, this is currently the intended behaviour from the point of view of React, yes?gaearon commentedon Aug 17, 2018
@jpkempf A separate issue is best for something that doesn't look like what's being described by this issue's title. Because otherwise it's unsearchable. Thanks.
jpkempf commentedon Aug 22, 2018
@gaearon sure, no problem! I've created #13459. This is my first issue so I hope everything's fine. :)
zibra commentedon Oct 9, 2018
same issue on my end
aldendaniels commentedon Dec 6, 2018
Also hitting this - and another simple repro case: https://codepen.io/anon/pen/NEQbjM?editors=1112
Here you can see that the native
focusoutevent does fire, but there's no corresponding syntheticonBlurevent.bcbane commentedon Sep 24, 2019
This is also affecting me
lukas1994 commentedon Feb 29, 2020
Any updates?
mx2323 commentedon May 23, 2020
ditto
maltenuhn commentedon Jul 3, 2020
Same here, we’d be happy to contribute towards a fix if there is appetite for one
Rheeseyb commentedon Jul 8, 2020
For those still hitting this, we came up with a workaround here: https://codesandbox.io/s/determined-euler-t7ijw?file=/src/App.js
In a nut shell, what we did was use a wrapper which:
onBlurevent handler directly to the underlying DOM element via auseEffect, requiring attaching a ref to theinputitselfforwardRefcombined with compose-react-refs to ensure we can still pass through a ref and also have access to a ref inside the wrapperuseEffectclean up to do this because I believe that happens during the reconciliation, meaning the event handler is removed before being called in this use case - I have verified this)There is one major caveat here though - because it requires attaching an event handler to the underlying element, it means that handler will be dealing with the raw event rather than React's
SyntheticEvent, which may be a deal breaker for you.To save you clicking the link, here's the code for the wrapper (which could equally have been written as a HOC):
fix: change onBlur React handler for vanilla onBlur method
Aetet commentedon Jun 10, 2022
Any progress on this issue?
bennettdams commentedon Oct 25, 2022
Another workaround for conditional rendering
I wanted to submit a form via
onBlurand encountered this bug. In my case, I was conditionally rendering either the data to display or a form to edit that data in the same place.Something like:
Not sure if obvious for everyone, but there is another workaround:
Don't render the elements conditionally, but apply the
display: noneCSS property to either one of them. That way, the elements won't get re-/mounted and hence theonBlurcallback is working as expected:feat: Save post changes on blur
sanduluca commentedon Jun 23, 2023
The problem still persists here is a snack with React Native
To reproduce:
https://snack.expo.dev/@sanduluca/react-onblur-events-not-firing-during-unmount
altyntsevlexus commentedon Mar 11, 2024
Here is a possible workaround. In my case I have a button that unmount component with input inside. So we can check an active element and trigger
blur()before unmount.For proper usage we need to check if the active element is an HTMLElement, not just Element.