How do I force a component to unmount when I navig

2020-04-08 14:06发布

问题:

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

回答1:

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



回答2:

Checkout React Navigation's ways to handle a component's lifecycle event in their docs here

TL;DR, you can checkout React Navigation's navigation lifecycle event to perform actions when you switch screens. Hope it helps.