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 fails to unmount component from within event handler #3298
According to what I see with the debugger, it seems to be because a SyntheticMouseEvent is trying to get dispatched. And I guess the target has just been unmounted...
Basically on user language change I want to add the new language to the React context, and force re-render of the whole thing.
This is not Flux code but I guess you'll understand what it does:
context.addEventListener(function(event) {
if ( event.name === AppEvents.Names.USER_PREFERRED_LANGUAGE_SELECTED ) {
setTimeout(function() {
var newLanguage = event.data.language;
var newReactContext = buildReactContext(newLanguage);
context.unmount();
context.setReactContext(newReactContext);
context.renderCurrentAtomState();
},0);
}
});
As you can see it works for me with a setTimeout, I get an error only when unmounting synchronously.
(This really happens during the unmount, if I remove the other calls, I still get the error)
Issue is related to mutating nodes synchronously within an event loop. We may want to make unmount take effect at the end of the event loop. I think this demonstrates the more general problem of how to handle these top-level functions from within an event loop.
We encountered this problem as well, but we've found a dirty work-around. If you wrap React.unmountComponentAtNode(target) in a setTimeout, this will work.
We use this in a portal mixin, which renders a React component in a different DOM element. This raised another problem when rendering a new component directly after unmounting the first one. In that case, the second component won't show up. We fixed this by storing a unique key in the component state and the DOM element, and only calling unmountComponentAtNode if these keys are the same.
setTimeout(()=> {
if (this.state.portalKey === this._target._portalKey) {
React.unmountComponentAtNode(this._target);
}
});
This works for now, but we'd love a less dirty, more native solution
Invariant Violation: React DOM tree root should always have a node reference.
I'll note that this does work if you unmount a subtree but apparently not if you unmount the whole root – because unmounting is batched differently from updates.
I'm guessing ReactTestUtils.SimulateNative.click isn't appropriate in this situation. Is there an existing pattern in the test suite that could reproduce?
@quicksnap Not sure why that wouldn't work. You could try Simulate.click which is generally preferred unless you're trying to test the event system code.
Activity
slorber commentedon Mar 2, 2015
You can find more informations on my usecase here:
#3038
Basically on user language change I want to add the new language to the React context, and force re-render of the whole thing.
This is not Flux code but I guess you'll understand what it does:
As you can see it works for me with a setTimeout, I get an error only when unmounting synchronously.
(This really happens during the unmount, if I remove the other calls, I still get the error)
syranide commentedon Mar 2, 2015
Unmounting during a React event is not currently supported AFAIK.
jimfb commentedon Mar 2, 2015
Unmounting as the result of a event sounds like a perfectly reasonable thing to want to do; I think we should try to support this.
@slorber can you create a simple jsfiddle that demonstrates the issue?
[-]Little bug when unmounting[/-][+]React fails to unmount component from within event handler[/+]slorber commentedon Mar 2, 2015
@jimfb Yes I've successfully reproduced the case here: http://jsfiddle.net/wevohwfp/
My first attempt was not working because I did not try to add another event listener to the parent
jimfb commentedon Mar 2, 2015
Awesome, thanks!
Issue is related to mutating nodes synchronously within an event loop. We may want to make unmount take effect at the end of the event loop. I think this demonstrates the more general problem of how to handle these top-level functions from within an event loop.
jasonslyvia commentedon Mar 11, 2015
plus one here, it's reasonable behaviour to unmount a component as the result of a React event.
blairanderson commentedon Jun 4, 2015
I just ran into this.
for anyone else coming here, stopPropagation on the event does not work either.
I ended up building a wrapper component to handle, basically acting as a mini router for the user-flow.
bramschulting commentedon Aug 28, 2015
We encountered this problem as well, but we've found a dirty work-around. If you wrap
React.unmountComponentAtNode(target)in asetTimeout, this will work.We use this in a portal mixin, which renders a React component in a different DOM element. This raised another problem when rendering a new component directly after unmounting the first one. In that case, the second component won't show up. We fixed this by storing a unique key in the component state and the DOM element, and only calling
unmountComponentAtNodeif these keys are the same.This works for now, but we'd love a less dirty, more native solution
lencioni commentedon Nov 12, 2015
Is this perhaps a duplicate of #2605?
sophiebits commentedon Apr 22, 2016
This now throws with v15 (http://jsfiddle.net/wevohwfp/1/):
I'll note that this does work if you unmount a subtree but apparently not if you unmount the whole root – because unmounting is batched differently from updates.
quicksnap commentedon Aug 12, 2016
I tried running the following test but could not produce the error: https://gist.github.com/quicksnap/10905082804338c9ac3f5db474dd303e
I'm guessing
ReactTestUtils.SimulateNative.clickisn't appropriate in this situation. Is there an existing pattern in the test suite that could reproduce?sophiebits commentedon Aug 18, 2016
@quicksnap Not sure why that wouldn't work. You could try Simulate.click which is generally preferred unless you're trying to test the event system code.
aweary commentedon Apr 21, 2017
This appears to work out of the box with Fiber: https://jsfiddle.net/5rg0wu8c/
plinehan commentedon Apr 5, 2018
For any other n00bs, Fiber is React v16.