Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a component to unmount when I navigate to a new page in react native?

I want to make sure the lifecycle method ComponentWillUnmount fires when I navigate to a new page. I found this post, but that doesn't seem to mention anything about navigating to a new page. I also found this post but I'm using react-navigation. In addition, I'm not using pop/push. I'm simply using this.props.navigation.navigate('SomePage') to get to the next page

like image 864
VK1 Avatar asked Mar 06 '19 08:03

VK1


People also ask

How do you force a component to unmount react?

Unmount a React Node React has a top-level API called unmountComponentAtNode() that removes a component from a specific container. The function unmountComponentAtNode() takes an argument as a container from which the specific component should be removed.

How do I manually unmount a component?

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.

How do you call a component unmount?

ReactJS componentWillUnmount() Method 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.

What causes component to unmount?

When rendering a function component, the state change causes the component to unmount and then remount. The same component using class based implementation won't unmount.


1 Answers

You can use this.props.navigation.replace('routName'); It will do your job.

like image 79
Sarmad Shah Avatar answered Oct 09 '22 02:10

Sarmad Shah