Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - When "componentWillUnmount" will be called?

Tags:

react-native

I'm using the Navigator in order to navigate between pages.

When Navigator.push() method is called, the "componentWillUnmount" method isn't called on the page we're navigating from.

It's a little bit problematic because I want to unsubscribe from listeners, etc.

Is it a normal behavior? When the "componentWillUnmount" is actually called?

like image 643
Rom Shiri Avatar asked Jun 05 '16 21:06

Rom Shiri


People also ask

When component will unmount is called?

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

When should I use componentWillUnmount?

componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount() .

How do you trigger in componentWillUnmount?

ReactJS componentWillUnmount() MethodThe 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 you check if the React component is unmounted?

The useEffect() hook is called when the component is mounted and sets the mounted. current value to true . The return function from the useEffect() hook is called when the component is unmounted and sets the mounted. current value to false .


1 Answers

Navigator uses stack to manager route. When you push a new page, current page wont unmount, if you pop from new page to the current page, the new page will unmount. And in other scene, if you render some child component based on some regulation like isLoading or other, the componentWillUnmount method will also be called when they no longer need to be rendered.

like image 101
liu pluto Avatar answered Oct 11 '22 06:10

liu pluto