question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unmount all root components from container

See original GitHub issue

Hi,

I’m using React for certain modules in a mature project which is primarily built around jQuery and server-side rendered HTML. The main routing mechanism loads a particular URL via $.ajax and then replaces the HTML of the content container of the page via $('#content).html(result).

When using React components on a page I noticed that I manually have to unmount the root components before leaving the page and loading a new page with $('#content).html(...). In that case the React components are rendered into a div deeply nested somewhere in the ‘#content’ container.

Unmounting the root components rendered into the content container manually via React.unmountComponentAtNode works as expected. But I’m concerned that developers could forget to unmount their components which could lead to memory leaks and unexpected behavior I guess.

Therefore I would like to unmount all possible root components in a generic way directly from the router. Unfortunately I don’t know how to get all the DOM nodes from a particular parent container in a generic way.

This doesn’t work because the components are not rendered directly into the content container but somewhere in the content container hierarchy:

React.unmountComponentAtNode(document.getElementById("content"));

Is there any way to retrieve all root components for a given DOM element? Selecting all elements with data-reactid would not only select the root components but also child components.

It would be helpful if React would directly support unmounting of a whole container.

Thanks in advance for your help and keep up the good work!

Benjamin

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
zpaocommented, Jul 29, 2015

This is doable in userland but it’s definitely awkward and requires some tree search to find all roots (or overselecting nodes - qSA('[data-reactid'] and check each for !node.parentNode.dataSet['reactid']).

It might be handy to expose roots in same way, this has come up before in other tooling. But it’s awkward to do. cc @sebmarkbage

So what I might suggest for you is an imperative wrapper around React.render - basically have something that tracks when you call render, store a reference to the target node, and then you can call into that to unmount. Here’s a really simple way you could do it.

var Renderer = {
  _nodes = [],
  render: function(element, node, cb) {
    Renderer._nodes.push(node);
    return React.render(element, node, cb);
  },
  unmount: function() {
    Renderer._nodes.forEach((node) = > React.unmountComponentAtNode(node));
  }
}

We actually use a very similar pattern at FB. I suspect that @sebmarkbage or @spicyj will probably close this and suggest something what I did.

0reactions
jimfbcommented, Jul 30, 2015

@winterbe React blog post will appear here: http://facebook.github.io/react/blog/

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to unmount all root level components in ReactJs
I would like to get all elements, other than AppContainer and then unmount them when my component upmounts, is this possible? enter image ......
Read more >
How to Unmount a ReactJS Node
React has a top-level API called unmountComponentAtNode() that removes a component from a specific container.
Read more >
Unmount a working container's root filesystem
Unmounts the specified containers' root file system, if no other processes are using it. Container storage increments a mount counter each time a...
Read more >
createRoot
Calling root.unmount will unmount all the components in the tree and “detach” React from the root DOM node. Once you call root.
Read more >
mranderson048.reagent.v0v8v0.reagent.dom
Returns the root DOM node of a mounted component. ... Note that force-update-all may not update root components. ... (render comp container callback)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found