Skip to content

Commit

Permalink
Fix crash unmounting an empty Portal (#14820)
Browse files Browse the repository at this point in the history
* Adds failing test for #14811

* Fix removeChild() crash when removing an empty Portal
  • Loading branch information
gaearon authored Feb 11, 2019
1 parent e15542e commit 1fecba9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
34 changes: 34 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFiber-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}>
Expand Down
10 changes: 5 additions & 5 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1fecba9

Please sign in to comment.