React Native: when you navigate away from a component, does that component ever get un-mounted?

送分小仙女□ 提交于 2020-07-09 17:10:39

问题


In my React Native app, if I have a stack navigator and I navigate from screenA to screenB with this.props.navigation.navigate('screenB'), is it possible that screenA would ever get un-mounted?

The reason I ask is that when screenA mounts, I make an API call that will load a resource that will be available in all screens via Redux. I might navigate to screenB while that API call is still in progress. So I want to know if it's possible that my navigating to screenB too soon could prevent the resource from loading and being available in screenB.

Any insight would be appreciated. Thanks!


回答1:


I'm assuming you are using react-navigation because you mentioned navigating by using this.props.navigation.navigate('screenB').

Assume the screen will never be unmounted. When you load a 'stack', componentDidMount will get triggered when you enter the screen for the first time, but it will not trigger if you go back to that screen, and componentWillUnmount will never trigger.

React Navigation does gives you a few helpers, you can see more in the docs here.



来源:https://stackoverflow.com/questions/62721409/react-native-when-you-navigate-away-from-a-component-does-that-component-ever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!