I need a function when change screen executing, I mean when from screen home navigate to about screen executing the callback function.
Scenario
I want make global stack when change any naviagte run callback function for some check like (userToken, internet state, check location is one or off, etc.)
Try this
i create a class component for Stack-navigation and implemnet UNSAFE_componentWillUpdate
, can detected any navigate.
Code
UNSAFE_componentWillUpdate(prevProp, prevState) {
if (prevProp != this.props) {
console.log('change state navigation');
}
}
Mistake this way
- Can not detected first screen
index-zero
Home-screen. - I must write for each stack this code.
My Stack
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
/>
<Stack.Screen
name="About"
component={About}
/>
</Stack.Navigator>
You can get the current route with "onNavigationStateChange" prop in "NavigationContainer"
React Navigation 4.x
React Navigation 5.x