Closed shahankit closed 6 years ago
Thanks!
Any reason to not call ReactDOM.unmountComponentAtNode(this.defaultNode)
before removing it too? I'm still having the issue described in #198 when not passing a node
.
Hi @bjoerge when you unmount Portal, the portal content are still visible? I tried replicating that in examples but couldn't get it to work. I also added a test case and it seems to work fine in my case:
ifReact(
'< 16',
test,
test.skip
)('should remove portal content for default node', () => {
document.body.innerHTML = '<div id="root"></div>';
ReactDOM.render(
<Portal>
<div>Foo</div>
</Portal>,
document.getElementById('root')
);
ReactDOM.unmountComponentAtNode(document.getElementById('root'));
expect(document.getElementById('root').innerHTML).toBe('');
});
I think a better approach would be to call ReactDOM.unmountComponentAtNode
first and then call document.body.removeChild
. This way the lifecycle hooks of Portal content will also be called.
componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.defaultNode || this.props.node);
if (this.defaultNode) {
document.body.removeChild(this.defaultNode);
}
this.defaultNode = null;
this.portal = null;
}
cc @tajo
Hi @shahankit! So my issue is not that the portal is still visible, but that componentWillUnmount
is never called on its child nodes (preventing me from doing cleanup tasks like removing listeners, etc).
I think your suggested approach here makes sense and will solve it 👍
Removes portal content using
ReactDOM.unmountComponentAtNode
for React < 16.