Animating when navigating with SwitchNavigator - react-native

Im using createSwitchNavigator to seperate my auth flow from the rest of the app. While navigating from one context to another, is there anyway to trigger an animation? The default behaviour is to replace the screen in place which is a little bit.. unceremonious.
An example would be navigating to App from a Login screen via this.props.navigation.navigate("Login")
const SwitchNavigator = createSwitchNavigator(
{
Login: AuthStackNavigator,
App: AppStackNavigator,
}
);
If not i'd be happy to listen to how you handle this scenario. Thanks a lot.

Related

how to get last navigation route when app is reloaded in react native?

I have a react-native app
and I define one splash screen. when user is Authenticated, it navigate to home and when user is not Authenticated, it navigate to login
its work correctly, but:
I want to when the app is in develop mode and when reloaded, it navigate to last route
can you help me to fix this ux problem?
You can access to the state of navigation, source here https://reactnavigation.org/docs/use-navigation-state/
import { useNavigationState } from '#react-navigation/native';
const usePreviousRouteName =() => {
return useNavigationState(state =>
state.routes[state.index - 1]?.name
? state.routes[state.index - 1].name
: 'None'
);
}

How to logout in react native?

I am junior in app development.
I just tried to logout in react native small project.
Then after I click the logout button, when I click the back button (Android device button), it goes to former screen again. Of course, the API doesn't work. In this case, how can I prevent to go back to the former screen?
logout = async () => {
await AsyncStorage.removeItem('userToken');
this.props.navigation.navigate('WelcomePage')
}
You can call reset instead of navigate.
The reset method lets us replace the navigator state with a new state:
navigation.reset({
index: 0,
routes: [{ name: 'Profile' }],
});
React Navigation provides createSwitchNavigator with Authentication in mind
Authentication Example

is there any way to navigate into two different App container react native

hi i"m new to react native i'm trying to build my first app
i'm having some trouble with react navigation I've done two different app container the first contain the startup Screen ,login,Signup, and Main which is the second app container that contain my other screenStacks now I've tried to implement a logout functionality but i couldn't managed how i can go back to the first app container that contain the login Screen
i'm using react navigation 3.0
i don't know if this is possible to manage ???
any help please ,thank you :)
-App
|
StarutUPScreen
SignIn
Signout
Main
....
-Main
|
mainScreen
other Screens
....
export default createAppContainer(
createSwitchNavigator(
{
StartUpScreen,
IntroOneScreen,
IntroTwoScreen,
IntroThreeScreen,
SignIn,
SignUp,
ForgotPassword,
Main (the other container)
},
{
initialRouteName: "StartUpScreen"
}
)
);
there should be only one app container
you can create multiple stack navigator and nest them together
const MainNavigator = createStackNavigator({...});
// this will have only screens from main stack
const RootNavigator = createStackNavigator({...,MainNavigator});
// root navigator will have auth part and then main stack
const AppContainer = createAppContainer(AppNavigator);
// Now AppContainer is the main component for React to render
export default AppContainer;

App Navigation state persisting EVEN IF app closed and restarted

I have a react-native app with two screens, Home and Details. Using react-navigation, Ive set the Stack navigator as following
const RootStack = createStackNavigator(
{
Home: FormComponent,
Details: DetailScreen
},
{
initialRouteName: "Home",
headerMode: "none"
}
);
Home contains a form, which once submitted, navigates to the Details screen with relevant data (using navigation.navigate("Details",{some data})). At this point, if I exit the app, and then open it again, the Details screen loads, with all the data preserved(Instead of the Home screen). I logged the navigation object data (this.props.navigation.) and it prints like the app was never closed.
Am I missing something here? Im new to React Native and Navigation, but from what I understand this is not expected behaviour.
Tried uninstalling app and rebuilding. This resets the app and Home screen loads. If I try reinstalling without uninstalling, back to same behaviour.
Tried also manually forcing navigation.goBack() on ComponentWillUnmount() but no difference.
This should've been a comment but sadly i don't have enough reputation.
Could you check if you haven't accidentally set a persistenceKey as a navigator prop?
https://reactnavigation.org/docs/en/state-persistence.html

React-native componentWillMount not calling

I am new in react-native I have two screens in my stack. Login and Home.
I want to go back to login from a button on home.
I am writing this code
this.props.navigation.navigate('loginScreen')
but in login screen componentWillMount method is not calling. I want to reset my form when user come on login screen from home.
Can anyone help me here?
Thanks in advance.
The this.props.navigation.navigate('loginScreen') don't work because you are now in loginScreen.
If you want to restart page this code isn't good. because have a loop!
correct code:
just when navigate to loginScreen from home use:
this.props.navigation.push('loginScreen')
NOT IN "componentWillMount"
To go back from login from home , you should use this.props.navigation.goBack() if the screen is immidiately before home.
Secondly, you should not use componentWillMount since it is deprecated and will be removed from React 17 onwards. Instead use componentDidMount
Since the component is already mounted therefore it won't call the react lifecycle events componentDidMount again. Therefore you should use the react-navigation listeners didFocus event.
didFocus: the screen focused (if there was a transition, the transition completed)
componentDidMount () {
this._onFocusListener = this.props.navigation.addListener('didFocus', (payload) => {
// Perform the reset action here
});
}
Since your Home and Login Screens are both under the same StackNavigator, when you go from Home back to Login the state stays the same as the component doesn't unmount. The recommended way to solve this is using the SwitchNavigator of react-navigation. Read this very helpful part of the documentation below
https://reactnavigation.org/docs/en/auth-flow.html
You may not be familiar with SwitchNavigator yet. The purpose of
SwitchNavigator is to only ever show one screen at a time. By default,
it does not handle back actions and it resets routes to their default
state when you switch away.
The perfect solution is with this.props.navigation.push('Login'), I tried with SwitchNavigator but it doesn't provide navigationOptions for header.