Skip to content

Commit

Permalink
Fix Portals not being unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Apr 13, 2019
1 parent c91f8ac commit 741bd51
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compat/src/index.js
Expand Up @@ -73,6 +73,9 @@ class ContextProvider {
function Portal(props) {
let wrap = h(ContextProvider, { context: this.context }, props.vnode);
render(wrap, props.container);
this.componentWillUnmount = () => {
render(null, props.container);
};
return null;
}

Expand Down
26 changes: 26 additions & 0 deletions compat/test/browser/portals.test.js
Expand Up @@ -51,4 +51,30 @@ describe('Portal', () => {
render(<App />, root);
expect(scratch.firstChild.firstChild.childNodes.length).to.equal(0);
});

it('should unmount Portal', () => {
let root = document.createElement('div');
let dialog = document.createElement('div');
dialog.id = 'container';

scratch.appendChild(root);
scratch.appendChild(dialog);

function Dialog() {
return <div>Dialog content</div>;
}

function App() {
return (
<div>
{createPortal(<Dialog />, dialog)}
</div>
);
}

render(<App />, root);
expect(dialog.childNodes.length).to.equal(1);
render(null, root);
expect(dialog.childNodes.length).to.equal(0);
});
});

0 comments on commit 741bd51

Please sign in to comment.