问题
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