Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Unable to use unmountComponentAtNode with unstable_createRoot #21441

Closed
Sawtaytoes opened this issue May 6, 2021 · 5 comments
Closed

Bug: Unable to use unmountComponentAtNode with unstable_createRoot #21441

Sawtaytoes opened this issue May 6, 2021 · 5 comments
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Enhancement

Comments

@Sawtaytoes
Copy link

When calling unmountComponentAtNode(document.getElementById('react-root')), it returns false.

This would normally happen because I'm trying to unmount something that's not a React node, but that's not the case.

I'm using unstable_createRoot. If I use render instead, it works fine.

React version: 0.0.0-experimental-79740da4c

Steps To Reproduce

  1. Render component with unstable_createRoot.
  2. Try to unmount the app with unmountComponentAtNode.

Link to code example: https://codesandbox.io/s/epic-leftpad-ih6ts?file=/src/index.js

The current behavior

Doesn't unmount the root React component.

The expected behavior

Should unmount the root React component.

@Sawtaytoes Sawtaytoes added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label May 6, 2021
@eps1lon
Copy link
Collaborator

eps1lon commented May 6, 2021

unmountComponentAtNode only works with legacy roots i.e. ReactDOM.render.

For the new roots you have the method directly on the root:

const root = ReactDOM.unstable_createRoot(rootElement)
root.render(<App />);
root.unmount();

Does this work for you?

Edit:
Though for migration purposes an additional warning similar to

if (__DEV__) {
const isModernRoot =
isContainerMarkedAsRoot(container) &&
container._reactRootContainer === undefined;
if (isModernRoot) {
console.error(
'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +
'passed to ReactDOM.createRoot(). This is not supported. Did you mean to call root.unmount()?',
);
}
would be nice.

@Sawtaytoes
Copy link
Author

Sawtaytoes commented May 6, 2021

Something like that would've saved me 2 hours tonight 😄. Definitely a reasonable solution.

I'd also say it should be on Reactjs.org:

image

This is the other place I checked:
https://reactjs.org/docs/concurrent-mode-adoption.html

@eps1lon
Copy link
Collaborator

eps1lon commented May 6, 2021

I'd also say it should be on Reactjs.org:

Documentation for experimental is not updated frequently and not viable considering the nature of these release channels.

@gaearon
Copy link
Collaborator

gaearon commented Jun 23, 2021

If you do unmountComponentAtNode() on a node that was previously only passed to createRoot then it does warn already.
https://codesandbox.io/s/awesome-euclid-sqhz1?file=/src/index.js

The case where it doesn't is if you render first, then unmountComponentAtNode, then createRoot on the same node, and then unmountComponentAtNode. This seems a bit like an unlikely combination to me. I don't think this is worth fixing but if this comes up again, we'll do that.

@gaearon gaearon closed this as completed Jun 23, 2021
@Sawtaytoes
Copy link
Author

Sawtaytoes commented Jun 23, 2021

@gaearon It has an error message now :P.

I think the situation you're talking about with the "render > unmount > render > unmount" is what I ran into.

I was rendering a React component in Node-RED Dashboard which, if you update a node, re-runs your entrypoint in the dashboard. I was making sure to unmount before remounting, but it was still mounting and unmounting multiple times in the same page load.

I later changed this whole system to render the React app into an iframe using the srcdoc attribute so there was only ever one mount on a given root node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Enhancement
Projects
None yet
Development

No branches or pull requests

3 participants