-1
In the React Navigation 5 no longer exists the Switch Navigator. In Upgrading Doc the proposed solution to prevent the user from going back to the login screen is this if ternary that checks if the User is logged in.
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
{isLoggedIn ? (
<>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</>
) : (
<Stack.Screen name="SignIn" component={SignInScreen} />
)}
</Stack.Navigator>
</NavigationContainer>
);
}
But of course I need to create that function isLoggedIn but I have no idea where to start. Someone has any suggestions?
isLoggedIn
would be a variable, aboolean
for example. Probably in your application you should already store something like this in the localStorage, a token, something like that.– Camilo Santos
It would be something like this:
async function isLoggedIn() {
 // buscando o user_id do localStorage
 const user_id = localStorage.getItem('user');
 
 })
. ???– Jorge Cardoso Rabello
Then you’d have to return something like
return user_id !== null
to return a Boolean, I think it would work that way. I usually leave some user information logged in using the Context API, so I just check if the variable in the state is true, so when vc logs in it redirects automatically.– Camilo Santos