I need to print a message when a screen is opened or closed. I am using useEffect
to show when the component mounts and when it unmounts. This is the code for useEffect
:
useEffect(() => {
console.log("Starting Home");
return () => {
console.log("Ending Home");
};
},[])
And the navigation in App.js
:
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="About" component={About} />
</Stack.Navigator>
</NavigationContainer>
Here, when I open the app, it automatically loads the Home screen. The app works properly with About page and prints messages when it is opened or closed. However, it prints the message for the home page only once when the app is opened.