From 56d2180a4bcd1fbdb3f1e7facf832dcd4e1b2f87 Mon Sep 17 00:00:00 2001 From: Khodor Ammar Date: Mon, 11 Feb 2019 10:27:41 +0400 Subject: [PATCH 1/2] Adds failing test for https://github.com/facebook/react/issues/14811 --- .../src/__tests__/ReactDOMFiber-test.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/react-dom/src/__tests__/ReactDOMFiber-test.js b/packages/react-dom/src/__tests__/ReactDOMFiber-test.js index 864caa0f3358f..c19777e7e56a3 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFiber-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFiber-test.js @@ -461,6 +461,40 @@ describe('ReactDOMFiber', () => { expect(container.innerHTML).toBe('
'); }); + 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 ( +
+ {this.state.show && ( + + {ReactDOM.createPortal(null, portalContainer)} +
child
+
+ )} +
parent
+
+ ); + } + } + + const instance = ReactDOM.render(, container); + expect(container.innerHTML).toBe( + '
child
parent
', + ); + instance.setState({show: false}); + expect(instance.state.show).toBe(false); + expect(container.innerHTML).toBe('
parent
'); + }); + it('should keep track of namespace across portals (simple)', () => { assertNamespacesMatch( From 646ad90f990eb7b94d45bc99c8dcda335bc31c86 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 11 Feb 2019 15:50:44 +0000 Subject: [PATCH 2/2] Fix removeChild() crash when removing an empty Portal --- packages/react-reconciler/src/ReactFiberCommitWork.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.js b/packages/react-reconciler/src/ReactFiberCommitWork.js index 85231dd068bf8..06ad89eaf1310 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.js @@ -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;