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.

Unmounting React node

See original GitHub issue

I’m trying to unmount a React.js node with this._rootNodeID

 handleClick: function() {

    React.unmountComponentAtNode(this._rootNodeID)

 }

But it returns false.

The handleClick is fired when I click on an element, and should unmount the root-node.

I’ve tried this as well:

  React.unmountComponentAtNode($('*[data-reactid="'+this._rootNodeID+'"]')[0])

That selector works with jQuery.hide(), but not with unmounting it, while the documentation states it should be a DOMElement, like you would use for React.renderComponent

After a few more tests it turns out it works on some elements/selectors.

It somehow works with the selector: document.getElementById('maindiv'), where maindiv is an element not generated with React.js, and just plain html. Then it returns true.

But as soon as I try and select a different ElementById that is generated with React.js it returns false. And it won’t work with document.body either, though they all essentially return the same thing if I console.log them (getElementsByClassName('bla')[0] also doesn’t work)

There should be a simple way to select the node via this, without having to resort to jQuery or other selectors, I know it’s in there somewhere…

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
UXnomaancommented, Sep 12, 2018

@gaearon – should unmountComponentAtNode be used to unmount a component that was created with React.createPortal?

1reaction
gaearoncommented, May 16, 2016

@amit-jamwal The node you passed there must be the same one that you previously passed to ReactDOM.render(). That’s what the warning says: you want to be unmounting the container node you renderered into, not a node managed by React that is defined in some component’s render() method. To unmount an inner node managed by React, use setState as described above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unmounting React.js node - Stack Overflow
Here is a simple JSFiddle where we mount the component and then unmount it after 3 seconds. Share.
Read more >
ReactJS unmountComponentAtNode() Method - GeeksforGeeks
In class-based components, the unmountComponentAtNode() method Remove a mounted React component from the DOM. Creating React Application: Step 1 ...
Read more >
unmountComponentAtNode - React Docs
Call unmountComponentAtNode to remove a mounted React component from a browser DOM node and clean up its event handlers and state. import {render, ......
Read more >
[Solved]-Unmounting React.js node-Reactjs
[Solved]-Unmounting React.js node-Reactjs ... Unmount components from the same DOM element that you mount them in. So if you did something like: ReactDOM.render(< ......
Read more >
Can't perform a react state update on an unmounted component
Mounting is the process of adding a component that exists in the form of a Virtual DOM node in memory to the browser...
Read more >

github_iconTop Related Medium Post

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