Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a component is "unmounted" will it ever again be used?

Tags:

reactjs

In react if a component is unmounted will that object ever again be reused?

Use case: let's say that component is sometime's shown from a higher level in an if statement.

If that component disappears a due to a state change then componentWillUnmount call will fire of course. When it later re-appears due to a state change, does react reuse this same component or does it create it from scratch? I would guess there is no guarantee of a new component, which is why both WillMount and WillUnmount exist.

like image 902
Parris Avatar asked Feb 09 '16 00:02

Parris


People also ask

What happens unmount components?

Purpose of this method is to destroy the side effects created by the component. Once component is unmounted, we can't use it again. Every time a new component gets created. Also if there is no difference in virtual dom and actual dom, react can stop the update phase as well.

Can I call component did mount again?

There is no call to ComponentDidMount. It is only called once after the initial render. Let's take a look at how componentDidMount() could be called many times.

What does unmounting a component mean?

Components are unmounted when the parent component is no longer rendered or the parent component performs an update that does not render this instance.

Is component will unmount deprecated?

componentWillUnmount() method has been deprecated, it is removed from the component life cycle. instead of you can use getderivedstatefromprops () life cycle method.


1 Answers

No, once it is unmounted, the instance will be garbage collected if there are no references, and separately, if a component is re-mounted, there will be a new instance.

https://github.com/facebook/react/issues/4770#issuecomment-136928760

like image 134
Nathan Hagen Avatar answered Sep 22 '22 23:09

Nathan Hagen