Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,34 @@ describe('ReactDOM', () => {
' in App (at **)',
]);
});

it('should not remove _reactRootContainer from a root if unmountComponentAtNode is follow by a render', () => {
const containerParent = document.createElement('div');
const containerApp = document.createElement('div');

function App1() {
return <div id="app1" />;
}
function App2() {
return <div id="app2" />;
}

class Parent extends React.Component {
render() {
return <div />;
}

componentDidMount() {
ReactDOM.render(<App1 />, containerApp);
ReactDOM.unmountComponentAtNode(containerApp);
ReactDOM.render(<App2 />, containerApp);
}
}

ReactDOM.render(<Parent />, containerParent);
// if null, the next unmountComponentAtNode / render might not behave correctly
// skipping some life cycle like componentWillUnmount of App2 etc.
expect(containerApp._reactRootContainer).not.toBeNull();
expect(containerApp.innerHTML).toEqual('<div id="app2"></div>');
});
});
8 changes: 7 additions & 1 deletion packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ function legacyRenderSubtreeIntoContainer(
callback,
);
} else {
if (root.shouldBeUnMount && children != null) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow will fail there, but waiting to get Proposal of fix: opinion

root.shouldBeUnMount = false;
}
root.render(children, callback);
}
}
Expand Down Expand Up @@ -679,9 +682,12 @@ const ReactDOM: Object = {
}

// Unmount should not be batched.
container._reactRootContainer.shouldBeUnMount = true;
DOMRenderer.unbatchedUpdates(() => {
legacyRenderSubtreeIntoContainer(null, null, container, false, () => {
container._reactRootContainer = null;
if (container._reactRootContainer.shouldBeUnMount) {
container._reactRootContainer = null;
}
});
});
// If you call unmountComponentAtNode twice in quick succession, you'll
Expand Down