-
Notifications
You must be signed in to change notification settings - Fork 46.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React onBlur events not firing during unmount #12363
Comments
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. |
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 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 |
@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. |
same issue on my end |
Also hitting this - and another simple repro case: https://codepen.io/anon/pen/NEQbjM?editors=1112 Here you can see that the native |
This is also affecting me |
Any updates? |
ditto |
Same here, we’d be happy to contribute towards a fix if there is appetite for one |
For those still hitting this, we came up with a workaround here: https://codesandbox.io/s/determined-euler-t7ijw?file=/src/App.js
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 To save you clicking the link, here's the code for the wrapper (which could equally have been written as a HOC):
|
Any progress on this issue? |
Another workaround for conditional renderingI wanted to submit a form via function Post({ isPostEditMode }: { isPostEditMode: boolean }): JSX.Element {
return (
<div>
<p>Post</p>
{isPostEditMode ? (
// Form
<form onBlur={...}>...</form>
) : (
// Data
<div>Data</div>
)}
</div>
)
} Not sure if obvious for everyone, but there is another workaround: Don't render the elements conditionally, but apply the // example uses Tailwind
function Post({ isPostEditMode }: { isPostEditMode: boolean }): JSX.Element {
return (
<div>
<p>Post</p>
{/* Form */}
<form
className={isPostEditMode ? 'hidden' : 'block'}
onBlur={...}
>
...
</form>
{/* Data */}
<div className={isPostEditMode ? 'block' : 'hidden'}>Data</div>
</div>
)
} |
We now use conditional display instead of conditional rendering. This is done so the form's `onBlur` is triggered, as there's a bug in React: facebook/react#12363
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 |
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
For proper usage we need to check if the active element is an HTMLElement, not just Element. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
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.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
https://codesandbox.io/s/134wrzy6q7
What is the expected behavior?
I would expect that, just like the browser fires a
focusout
event when removing a DOM node, React would fire anonBlur
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.
The text was updated successfully, but these errors were encountered: