Fix crash unmounting an empty Portal (#14820)

* Adds failing test for https://github.com/facebook/react/issues/14811

* Fix removeChild() crash when removing an empty Portal
This commit is contained in:
Dan Abramov authored and GitHub committed 2019-02-11 18:37:53 +00:00
1 parent e15542ee0f
commit 1fecba9230
2 files changed
+39 -5

No files matched your search

+34
View File
@@ -461,6 +461,40 @@ describe('ReactDOMFiber', () => {
expect(container.innerHTML).toBe('<div></div>');
});
it('should unmount empty portal component wherever it appears', () => {
const portalContainer = document.createElement('div');
class Wrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
show: true,
};
}
render() {
return (
<div>
{this.state.show && (
<React.Fragment>
{ReactDOM.createPortal(null, portalContainer)}
<div>child</div>
</React.Fragment>
)}
<div>parent</div>
</div>
);
}
}
const instance = ReactDOM.render(<Wrapper />, container);
expect(container.innerHTML).toBe(
'<div><div>child</div><div>parent</div></div>',
);
instance.setState({show: false});
expect(instance.state.show).toBe(false);
expect(container.innerHTML).toBe('<div><div>parent</div></div>');
});
it('should keep track of namespace across portals (simple)', () => {
assertNamespacesMatch(
<svg {...expectSVG}>
+5 -5
View File
@@ -1038,12 +1038,12 @@ function unmountHostComponents(current): void {
}
// Don't visit children because we already visited them.
} else if (node.tag === HostPortal) {
// When we go into a portal, it becomes the parent to remove from.
// We will reassign it back when we pop the portal on the way up.
currentParent = node.stateNode.containerInfo;
currentParentIsContainer = true;
// Visit children because portals might contain host components.
if (node.child !== null) {
// When we go into a portal, it becomes the parent to remove from.
// We will reassign it back when we pop the portal on the way up.
currentParent = node.stateNode.containerInfo;
currentParentIsContainer = true;
// Visit children because portals might contain host components.
node.child.return = node;
node = node.child;
continue;