how to clear screen inside drawer navigator in react native - react-native

const drawerNavigator = createDrawerNavigator({
HomeScreen: {
screen: customstack,
navigationOptions: {drawerLabel: () => null}
},
ContactAdmin : {screen : ContactAdmin},
ResetPasswordScreen: {
screen: ResetPassword,
navigationOptions: {drawerLabel: 'Reset Password',}
},
LogoutScreen: {
screen: Logout,
navigationOptions: {drawerLabel: 'Logout',}
},
NoNetwork : { screen : NoNetwork }
},
{
initialRouteName: 'HomeScreen',
gesturesEnabled: true,
style: {
leftDrawerWidth: 40,
},
drawerPosition :"left", contentComponent:SideMenu,
contentOptions: {
activeTintColor: '#e91e63',
},
navigationOptions: ({ navigation }) => ({
headerTitle: (
<View style={{flexDirection : 'row',flex :1,justifyContent:'center'}}>
<Image style={{width: 70, height: 40,backgroundColor:'white'}} source={require('./logo.png')} />
</View>
),
headerLeft:(<TouchableOpacity onPress={() =>navigation.toggleDrawer()}>
<Image style={{width: 25, height: 25,marginLeft:15}} source={require('./menu.png')} />
</TouchableOpacity>),
headerStyle: {
backgroundColor: '#008000'
}
})
},
);
const RootStack = createStackNavigator({
SplashScreen: { screen: SplashScreen },
Login: { screen: Login },
Signup: {screen: Signup},
Home: {screen: drawerNavigator,
},
}, {
initialRouteName: 'SplashScreen',
})
const AppNavigator = createAppContainer(RootStack)
export default AppNavigator;
Above code is used as a navigator for my application.Now in drawer navigator when i goes inside reset password and after successful operation i send to login screen like this.
await AsyncStorage.removeItem('uname', (err) => {
this.props.navigation.navigate("Login")
});
Uptil now everthing is working fine.Main problem arises when after entering new credentials in login screen.It does not takes me to HomeScreen but on resetpassword screen only.Why this is happening.After successful login i called
this.props.navigation.navigate("Home")
But then also it is going in reset password.How can i clear drawernavigation so that again after re logging with new password it don't take to previous screen i.e. ResetPassword

You will need to do a reset, here is an example from their documentation on how to do this. We want to do this because we want to prevent from going back to the previous state and instead start with a brand new navigation state.
import { StackActions, NavigationActions } from 'react-navigation';
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Profile' })],
});
this.props.navigation.dispatch(resetAction);
And a link here to the said example: https://reactnavigation.org/docs/en/stack-actions.html

Related

How to navigate from login screen to home screen that contains the bottom tabs

Please help , im not sure how to make this work,
I dont know how to navigate from the login page to the home screen that will contain tabs, i only know how to navigate from the login to the home screen, but without the bottom tabs.
The error i get is: The component for route 'App'must be a React component.
const HomeStack = createStackNavigator(
{
//Defination of Navigaton from home screen
Home: { screen: HomeScreen },
ViewBookings: {
screen: ViewBookingsScreen,
navigationOptions: {
//Header customization of the perticular Screen
headerStyle: {
backgroundColor: '#0892d0',
},
headerTintColor: '#FFFFFF',
title: 'View All Bookings',
//Header title
},
},
},
{
defaultNavigationOptions: {
//Header customization of the perticular Screen
headerStyle: {
backgroundColor: '#0892d0',
},
headerTintColor: '#FFFFFF',
title: 'Welcome, User',
//Header title
},
}
);
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
const App = createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: TabStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
const TabStack = createBottomTabNavigator(
{
Home : { screen: HomeStack },
Bookings: { screen: BookingStack},
Reminders: { screen: ReminderStack},
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let IconComponent = Ionicons;
let iconName;
if (routeName === 'Home') {
iconName = `ios-home`;
} else if (routeName === 'Bookings') {
iconName = `ios-book`;
} else if (routeName === 'Reminders') {
iconName = `ios-alarm`;
}
return <IconComponent name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: '#0892d0',
inactiveTintColor: 'gray',
},
}
);
export default createAppContainer(App);
Once you authentication is successful you must call
this.props.navigation.navigate("Home");
This will navigate user to home screen.
You can decide route as per your requirement
In Your Home Screen If you are importing your Login Component like
import {Whatever} from 'Wherever'
Change it to
import Whatever from 'Wherever'
Try by removing curly braces. Because as i see you have used Default with export. So when we used default we dont use braces while importing.

Use the same custom header in all pages in React Native using AppContainer problems

I'm using react-native with react-navigation, and I'm hitting this issue with my screens not rendering correctly. I'm referencing this link to use the same disappearing header as a wrapper around my AppContainer:
https://medium.com/appandflow/react-native-scrollview-animated-header-10a18cb9469e
The first screen AuthLoadingScreen shows up fine, and then is supposed to navigate to the either of the stack's based on whether the user is verified. But for some reason, nothing shows up after the user is verified or not. I have a console.log upon entering both stacks and they both fire fine, just nothing gets rendered... any ideas are appreciated.
Here are some snippets of code that I have so far:
<- App.js ->
<View style={styles.fill}>
<Animated.ScrollView
style={styles.fill}
scrollEventThrottle={1}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
{ useNativeDriver: true },
)}
>
<-----My AppContainer----->
<AppContainer />
<------------------------->
</Animated.ScrollView>
<Animated.View
pointerEvents="none"
style={[
styles.header,
{ transform: [{ translateY: headerTranslate }] },
]}
>
<Animated.Image
style={[
styles.backgroundImage,
{
opacity: imageOpacity,
transform: [{ translateY: imageTranslate }],
},
]}
source={require('./assets/images/casino/casino-10.jpg')}
/>
</Animated.View>
</View>
<-My App Container consists of->:
import AuthLoadingScreen from '../screens/AuthLoadingScreen';
const AppStack = createStackNavigator({
Home: { screen: HomeScreen },
Settings: { screen: SettingsScreen },
OfferCategories: { screen: OfferCategoriesScreen },
});
const AuthStack = createStackNavigator({
Login: LoginScreen
});
export default createAppContainer({createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
})});
You can do it with two options: Warp in the AppContainer, or define the header section inside the top level of navigators:
lets start with the first option:
you can do
const AppContainerNavigator = createAppContainer({createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
})});
export default class AppContainer extends Component {
render() {
return (
<YOUR_HEADER_COMPONENT>
<AppContainerNavigator
ref={nav => {
this.navigator = nav;
}}
/>
</YOUR_HEADER_COMPONENT>
)
}
}
Or the other one is to pass the header component to your navigator while define it something like the following:
let MY_HEADER_COMPONENT = {
headerLeft: () => {
return null;
},
header: () => {
return <YOUR_HEADER_COMPONENT></YOUR_HEADER_COMPONENT>;
},
headerRight: () => {
return null;
}
};
export default createStackNavigator({
AuthLoading:{
screen: AuthLoadingScreen,
navigationOptions: ({
navigation
}) => (MY_HEADER_COMPONENT)},
App:{
screen: AppStack,
navigationOptions: ({
navigation
}) => (MY_HEADER_COMPONENT)},
....
})
Hopefully this answers your question

Logging out to a specific view in react native

I'm a beginner to react native development. At the moment I'm trying to set my navigation for the login logout process. This is my app flow right now Home -> SignIn -> Dashboard. Now when the user clicks the logout button in the dashboard it should go back to Home screen. This works well for the first time, but the problem is, if a user is already logged in, it skips the Home and SignIn and goes straight to Dashboard and if I try to log out from there it actually pushes to new Home view while keeping Dashboard in the stack and when I SignIn it does a pop transition to Dashboard showing the old state data. I did some digging and its said that react doesn't have provision to pop up out to specific view and we may have to use a hack to replace the screen but I'm not sure how to proceed on that. Below is my current code for the stack navigator and logout process.
const Navigator = createStackNavigator({
SplashScreen: {
screen: SplashScreen,
navigationOptions: {
header: null
}
},
Home: {
screen: Home,
navigationOptions: ({ navigation }) => ({
header: null
})
},
SignIn: {
screen: SignIn,
navigationOptions: {
header: null
}
},
Dashboard: {
screen: Dashboard,
navigationOptions: {
header: null
}
}
});
const Drawer = DrawerNavigator(
{ Navigator: { screen: Navigator } },
{ contentComponent: NavigationDrawer }
);
//In Dashboard
logoutUser = () => {
//Perform API call to logout user
this.props.navigation.navigate("Home");
}
You can use the SwitchNavigator,
const DrawerNavigatorExample = createDrawerNavigator({
//Drawer Optons and indexing
Screen1: {
//Title
screen: FirstActivity_StackNavigator,
navigationOptions: {
drawerLabel: 'Demo Screen 1',
drawerIcon: <Image
source={require('../assets/drawer.png')}
style={{ width: 25, height: 25, marginLeft: 5 }}
/>
},
},
Screen2: {
//Title
screen: Screen2_StackNavigator,
navigationOptions: {
drawerLabel: 'Demo Screen 2',
drawerIcon: <Image
source={require('../assets/drawer.png')}
style={{ width: 25, height: 25, marginLeft: 5 }}
/>
},
},
},
{
contentComponent: DrawerContent
});
const onboardingStackNavigator = createStackNavigator( {
Home: {
screen: Login,
navigationOptions: ({ navigation }) => ({
title: "Login",
headerTintColor: 'white',
headerBackTitle: 'Back',
headerStyle: {
backgroundColor: headerBackgroundColor,
},
headerTintColor: headerTintColor,
})
},
Signup: {
screen: Signup,
navigationOptions: ({ navigation }) => ({
title: "SignUp",
headerTintColor: 'white',
headerBackTitle: 'Back',
headerStyle: {
backgroundColor: headerBackgroundColor,
},
})
},},
{
initialRouteName: 'Home',
}
)
const AppNavigator = createAnimatedSwitchNavigator(
{
onboarding:onboardingStackNavigator,
Drawer: {
screen: DrawerNavigatorExample
}
},
{
initialRouteName: 'onboarding',
transition: (
<Transition.In type='fade'></Transition.In>
)
},
);
const AppContainer = createAppContainer(AppNavigator);
export default AppContainer;

How to force drawer to be over header with react navigation

I saw other answers regarding this issue but I can't find solution.
I want to show drawer always above app screen and above header. Instead drawer is always bellow header.
What am I doing wrong here:
const AppNavigation = createStackNavigator(
{
Main: { screen: Main, navigationOptions: {
title: "Main"
} },
Home: { screen: Home, navigationOptions: {
title: "Home"
} }
},
{
initialRouteName: "Main"
}
);
const DrawerNavigation = createDrawerNavigator(
{
Home: Home,
Main: Main
},
{
initialRouteName: "Main"
}
);
App = createStackNavigator({
drawer: {
screen: DrawerNavigation,
},
app: {
screen: AppNavigation
}
}, {
initialRouteName: 'drawer',
navigationOptions: ({navigation}) => ({
headerStyle: {backgroundColor: '#a9a9a9'},
title: 'Welcome!',
headerTintColor: 'white',
headerLeft: <Text onPress={() =>
navigation.dispatch(DrawerActions.toggleDrawer())}>Menu</Text>
})
});
export default () => (
<View style={{ flex: 1 }}>
<App />
</View>
Our app is similar to this. You can make the drawer be over the header with react-navigation if it is the <Root /> component of your app (the one you import into index.js). For example:
export const Root = DrawerNavigator({
Tabs: {
screen: Tabs,
title: 'Tabs'
},
STACK1: {
screen: STACK1,
title: 'STACK1',
},
STACK2: {
screen: STACK2,
title: 'STACK2',
},
});
That will give you something like this:

`goBack` in react navigation doesn't go back

Here is my navigation.js:
export const BottomTabs = createBottomTabNavigator({
'Specials': {
screen:SpecialsScreen
},
'Menus': {
screen:MenusScreen
},
});
export const Drawer = DrawerNavigator({
Home: { screen: Home, navigationOptions: {drawerLabel:() => null} },
Explore: { screen: BottomTabs },
'Support & Contact': { screen: ContactScreen },
Settings: { screen: Settings, navigationOptions: {drawerLabel:() => null} },
TOU: { screen: TOU, navigationOptions: {drawerLabel:() => 'Terms of Use'} },
Privacy: { screen: Privacy, navigationOptions: {drawerLabel:() => 'Privacy Policy'} },
Disclaimer: { screen: Disclaimer },
});
So the app launches and you get theHome screen:
App.js
render(){
return(<Provider store={store}><Drawer /></Provider>)
}
From there you click 'I agree' to terms of use and get sent to Explore screen:
HomeScreen.js
<TouchableOpacity onPress={() => {
this.props.navigation.navigate('Specials');
}}>
From Specials I hit the hamburger icon to toggle my drawer and go to Support & Contact from which there is a back button that I want to take me back to the previous screen:
ContactScreen.js
<TouchableOpacity onPress={() => {
this.props.navigation.goBack();
}}>
But this takes me to Home and not Explore or Specials like I would imagine. What did I miss?
Had to add stack navigators
const AppStack = createStackNavigator({ App: SpecialsScreen, MenusScreen });
const HomeStack = createStackNavigator({ Home });