Skip to content

Bug: Unable to use unmountComponentAtNode with unstable_createRoot #21441

Closed

Description

@Sawtaytoes

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.

Activity

  1. eps1lon commented on May 6, 2021

    @eps1lon
    Collaborator

    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 https://github.com/facebook/react/blob/bd070eb2c489a1f758e4a55b193820af7346fa15/packages/react-dom/src/client/ReactDOMLegacy.js#L345-L354 would be nice.

  2. Sawtaytoes commented on May 6, 2021

    @Sawtaytoes
    Author

    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

  3. eps1lon commented on May 6, 2021

    @eps1lon
    Collaborator

    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.

  4. gaearon commented on Jun 23, 2021

    @gaearon
    Collaborator

    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.

  5. Sawtaytoes commented on Jun 23, 2021

    @Sawtaytoes
    Author

    @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions