Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmounting React.js node

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. Documentation on unmountComponentAtNode here

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 wont 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..

like image 457
TrySpace Avatar asked Feb 09 '14 17:02

TrySpace


People also ask

What does unmounting mean in React?

The main job of React is to figure out how to modify the DOM to match what the components want to be rendered on the screen. React does so by "mounting" (adding nodes to the DOM), "unmounting" (removing them from the DOM), and "updating" (making changes to nodes already in the DOM).

How do you destroy component React?

The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document Object Model). This method is called during the Unmounting phase of the React Life-cycle i.e before the component gets unmounted.

How do I force a component unmount?

All you have to do is remove it from the DOM in order to unmount it. As long as renderMyComponent = true , the component will render. If you set renderMyComponent = false , it will unmount from the DOM.

Should I unmount component?

ReactJS – componentWillUnmount() MethodThis method is called during the unmounting phase of the React Lifecycle, i.e., before the component is destroyed or unmounted from the DOM tree. This method is majorly used to cancel all the subscriptions that were previously created in the componentWillMount method.


Video Answer


1 Answers

Unmount components from the same DOM element that you mount them in. So if you did something like:

ReactDOM.render(<SampleComponent />, document.getElementById('container')); 

Then you would unmount it with:

ReactDOM.unmountComponentAtNode(document.getElementById('container')); 

Here is a simple JSFiddle where we mount the component and then unmount it after 3 seconds.

like image 187
Michael LaCroix Avatar answered Sep 18 '22 18:09

Michael LaCroix