Navigation Header not hiding in react native - react-native

We are showing navigation side drawer in our react native application.
So, In that, For particular screen, We have to disable gestures.
And for complete app, We are hiding navigation header too.
MyStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if ((navigation.state.index === 2) || (navigation.state.index === 3)) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode,
header: null,
headerVisible: false,
};
};
const MyStack = createStackNavigator({
screen1: { screen: screen1, navigationOptions: { header: null, headerMode: 'none' } },
screen2: { screen: screen2, navigationOptions: { header: null, headerMode: 'none'} },
screen3: { screen: screen3, navigationOptions: { header: null, headerMode: 'none'} },
.
.
});
const MyAppStack = createAppContainer(createDrawerNavigator({
MyStack: {
screen: MyStack,
},
login: {
screen: login,
},
.
.
.
));
But, Header is still showing for all the screens.
Even we have tried for individual screen navigation header hiding, But, still not hiding.
class login extends Component<Props> {
static navigationOptions = {
header: null
};
Any suggestions?

To hide navigation header, add headerMode outside of navigationOptions. Both should be at same level.
export default createStackNavigator({
...
}, {
headerMode: 'none',
navigationOptions: {
...
}
});
Update 1
const MyAppStack = createAppContainer(createDrawerNavigator({
MyStack: {
screen: MyStack,
navigationOptions: {
header:false, // or drawerLabel: () => null
}
},
login: {
screen: login,
},
.
.
.
));

Hello Anil please try following code
MyStack.navigationOptions = ({ navigation }) => {
let drawerLockMode = 'unlocked';
if ((navigation.state.index === 2) || (navigation.state.index === 3)) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode,
header: null,
headerVisible: false,
};
};
const MyStack = createStackNavigator({
screen1: { screen: screen1 },
screen2: { screen: screen2 },
.
}, {
headerMode: 'none',
navigationOptions: {
headerVisible: false,
});

Finally, After somany forums read and some practice, Found the best solution to restrict gestures/swipe for particular screens in React-Native Navigation. Hope this will helps someone in future.
Install following library
import getCurrentRouteName from 'react-navigation-current-route';
const MyStack = createStackNavigator({
screen1: { screen: screen1, navigationOptions: { header: null, headerMode: 'none' } },
screen2: { screen: screen2, navigationOptions: { header: null, headerMode: 'none'} },
screen3: { screen: screen3, navigationOptions: { header: null, headerMode: 'none'} },
.
.
});
MyStack.navigationOptions = ({ navigation }) => {
const currentRoute = getCurrentRouteName(navigation.state);
let drawerLockMode = 'unlocked';
if ((currentRoute === 'screen1') || (currentRoute === 'screen2')) {
drawerLockMode = 'locked-closed';
}
return {
drawerLockMode
};
};
const MyAppStack = createAppContainer(createDrawerNavigator({
MyStack: {
screen: MyStack,
},
login: {
screen: login,
},
.
.
.
));

Related

Unable to pass a param when navigation to a different tab

I have looked every related topic on StackOverflow without success.
I am trying to go from a tab to another using "navigation.navigate" while passing a param. The navigation works but the param does not get passed.
here is what my TabNavigator looks like:
const handleTabPress = ({ navigation }) => {
navigation.navigate(navigation.state.routeName);
navigation.popToTop();
};
export default createBottomTabNavigator(
{
Overview: {
screen: OverviewStack,
navigationOptions: {
title: "Home",
tabBarOnPress: handleTabPress,
gesturesEnabled: false,
},
},
Profile: {
screen: ProfileStack,
resetOnBlur: true,
navigationOptions: {
title: "Profile",
tabBarOnPress: handleTabPress,
gesturesEnabled: false,
},
},
....
And my profileStack looks like this
const MainProfileStack = createStackNavigator(
{
...ProfileRoutes,
},
{
initialRouteName: "Profile",
resetOnBlur: true,
defaultNavigationOptions: ({ navigation }) => ({
...SharedHeader(navigation),
}),
}
);
const ProfileStack = createStackNavigator(
{
ProfileMain: MainProfileStack,
},
{
mode: "modal",
headerMode: "none",
}
);
You can always use redux to share data.

How to use nested navigation stacks in react native

Below is my navigation structure.
*Drawer Navigator (
Dashboard
StackNavigator({
Login
Welcome
})
Profile
StackNavigator({
Profile
})
)*
I have a logout button on the Profile page and on click on it I want to take the user back to the login page.
Please help.
Below is my Navigation.js file
const LoginNavigator = createStackNavigator({
Login: LoginScreen,
DashboardScreen: {
screen: DashboardScreen,
},
CaptureWeightScreen: {
screen: CaptureWeightScreen,
navigationOptions: {
headerBackTitle: " ",
headerTintColor: "#000"
}
}
}, {
navigationOptions: {
drawerLabel: 'Dashboard'
}
});
const ProfileNavigator = createStackNavigator({
ProfileScreen: ProfileScreen
}, {
navigationOptions: {
drawerLabel: 'Profile'
}
});
const MainNavigator = createDrawerNavigator({
DashboardNavigator: LoginNavigator,
ProfileNavigator: ProfileNavigator,
}, {
drawerBackgroundColor: Constants.buttonColor
});
export default createAppContainer(MainNavigator)
this.props.navigation.navigate({ routeName: "Login", params: { } })
Is the answer.
Thanks

I have a problem in my code when updating react-navigation v2 to v3

I want to update the react-navigation library V2 to V3 and change part of my code thinking that there would not be any problems but it turns out that I have problems creating createStackNavigator with a screen of type createDrawerNavigator and that in turn contains createBottomTabNavigator.
my code that works with the previous version was:
export const createRootNavigator = (signedIn = false) => {
const commonNavigationOptions = {
headerStyle: {
shadowColor: 'transparent',
elevation: 0
},
headerTintColor: DEFAULT_THEME.topaz
};
const SignedIn = createStackNavigator(
{
Home: {
screen: Drawer('Home'),
navigationOptions: () => ({
headerStyle: {
height: 0
},
header: getSafeArea(DEFAULT_THEME.backgrouncolorHomeSafeArea)
})
},
Cards: {
screen: Tabs('Cards'),
navigationOptions: () => ({
headerStyle: {
height: 0
}
})
},
);
const SignedOut = createStackNavigator(
{
SignIn: {
screen: LoginContainer,
navigationOptions: () => ({
headerStyle: {
height: 0
},
header: getSafeArea(DEFAULT_THEME.dark)
})
},
SelectableCardsList: { screen: SelectableCardsListComponent },
);
return createSwitchNavigator(
{
SignedIn: { screen: SignedIn },
SignedOut: { screen: SignedOut }
},
{
initialRouteName: signedIn ? 'SignedIn' : 'SignedOut'
}
);
};
const Drawer = (initialRoute) => createDrawerNavigator(
{
Home: { screen: Tabs('Home') },
{
initialRouteName: initialRoute,
contentComponent: CustomDrawerComponent
}
);
const Tabs = (initialRouteName) => createBottomTabNavigator(
{
Home: {
screen: HomeContainer,
navigationOptions: {
tabBarLabel: I18n.t('tabs.me')
}
},
Home2: {
screen: Home2,
navigationOptions: {
tabBarLabel: I18n.t('tabs.credentials')
}
},
{
initialRouteName: initialRouteName,
tabBarComponent: ({ navigation }) => <CustomBottomBarComponent navigation={navigation} navigationState={navigation['state']} />,
tabBarOptions: {
style: {
backgroundColor: 'white'
}
}
}
);
try this solution with react-navigation V3 and send me an error
I try the following:
encapsulate createSwitchNavigator in createAppContainer and separate (SignedOut and SignedOut) createStackNavigator out of createSwitchNavigator, the rest is still the same.
export const createRootNavigator = (signedIn = false) => createAppContainer(createSwitchNavigator(
{
SignedIn: { screen: SignedIn },
SignedOut: { screen: SignedOut }
},
{
initialRouteName: signedIn ? 'SignedIn' : 'SignedOut'
}
));
I get the following error: The component for route 'Home' must be a react component. For Example
import MyScreen from './MyScreen';
...
Home : MyScreen,
}
you can also use a navigator:
The problem is located in this part:
const SignedIn = createStackNavigator(
{
Home: {
screen: Drawer,
Also try to change Drawer for any component (a component with a blank screen) and this works, but I can not insert the Tabs in the Drawer.
Thank you very much for your help.

How to disable the drawer from react navigation for a certain screen

I've seen a lot of stack overflow articles that suggest on how to do this however I tried all of them and none of them seem to work for my case.
my target:
to be able to disable the drawer from all of my screens except my JobFeedScreen.
What I already did:
drawerLockMode: 'locked-closed, drawerLockMode: 'locked-open'
I also tried to put it on the screen itself using
static navigationOptions = ({ navigation }) => {
const { state } = navigation;
const {} = state;
return {
header: null,
drawerLockMode: 'locked-closed' ( or drawerLockMode: 'locked-open')
};
}
here are my codes
for my drawer
AppNavigation.js
//DRAWER NAVIGATOR
const DrawerNav = createDrawerNavigator({
JobFeed: {
screen: JobFeedScreenStack,
navigationOptions: ({navigation})=>({
drawerLabel: 'Job Feed',
drawerIcon: ({ tintColor }) => (
<SimpleLineIcons name="briefcase" size={ Metrics.icons.tiny } color={Colors.heavyGray} />
)
})
},
NotificationScreen: {
screen: NotificationScreenStack,
navigationOptions: {
drawerLabel: 'Notifications',
drawerIcon: ({ tintColor }) => (
<Entypo name = "notification" size = {15} color = {Colors.heavyGray} tintColor={Colors.white}/> )
}
},
ApplicationsFeed: {
screen: ApplicationScreenStack,
navigationOptions: {
drawerLabel: 'Applications',
drawerIcon: ({ tintColor }) => (
<Foundation name = "clipboard-notes" size = {15} color = {Colors.heavyGray} tintColor={Colors.white}/> )
}
},
},
{
initialRouteName: 'JobFeed',
drawerPosition: 'left',
contentComponent: CustomDrawerComponent,
drawerType: 'slide',
drawerLockMode: 'locked-closed',
});
other codes in AppNavigation.js:
// Manifest of possible screens
const AuthStackNavigation = createStackNavigator({
LaunchScreen: {
screen: LaunchScreen,
},
JobFeedScreen: {
screen: JobFeedScreen,
},
FilterScreen: {
screen: FilterScreen,
},
NotificationScreen: {
screen: NotificationScreen,
},
ApplicationScreen: {
screen: ApplicationScreen,
}
},
{
// Default config for all screens
initialRouteName: 'JobFeedScreen',
})
//JOB FEED SCREEN
const JobFeedScreenStack = createStackNavigator({
JobFeedScreen: {
screen: JobFeedScreen,
},
})
//LAUNCH SCREEN
const LaunchScreenStack = createStackNavigator({
LaunchScreen: LaunchScreen,
})
//NOTIFICATION SCREEN
const NotificationScreenStack = createStackNavigator({
NotificationScreen: NotificationScreen,
})
//APPLICATION SCREEN
const ApplicationScreenStack = createStackNavigator({
ApplicationScreen: ApplicationScreen,
})
//FILTER SCREEN
const FilterScreenStack = createStackNavigator({
FilterScreen: FilterScreen
})
// MAIN NAVIGATION
const MainNavigation = createSwitchNavigator({
DrawerNav: DrawerNav,
AuthStackNavigation: AuthStackNavigation,
})
export default createAppContainer(MainNavigation)

How to disable Drawer in nested component in React Navigation

Consider the render of the Main component:
render() {
const { isAuthenticated } = this.props;
return (
<View>
{isAuthenticated ? <Dashboard /> : <Login />}
</View>
);
I want to lock the drawer in the Login component. Now i know that i could achieve this if Login wasn't a child of Main this way (in my Router component):
Login: {
screen: Login,
navigationOptions: () => ({
drawerLockMode: 'locked-closed',
}),
},
But since Login is a child of Main and Main has the drawer, Login will automatically have the drawer too. I've tried "overriding" it by calling this in Login:
static navigationOptions = {
drawerLockMode: 'locked-closed',
};
But no success. Here's my Router:
const Stack = {
Main: { screen: Main },
Login: {
screen: Login,
navigationOptions: () => ({
drawerLockMode: 'locked-closed',
}),
},
Outbox: { screen: Outbox },
Dashboard: { screen: Dashboard },
JobList: { screen: JobList },
CreateJob: { screen: CreateJob },
Reporting: { screen: Reporting },
JobDescription: { screen: JobDescription },
};
const DrawerRoutes = {
DrawerStack: {
name: 'DrawerStack',
screen: StackNavigator(
Stack,
{
initialRouteName: C.MAIN,
headerMode: 'none',
navigationOptions: {
gesturesEnabled: false,
},
}),
},
};
export const DrawerNavigation = StackNavigator({
Drawer: {
name: 'Drawer',
screen: DrawerNavigator(DrawerRoutes, {
contentComponent: DrawerPanel,
}),
},
...Stack,
}, { headerMode: 'none' });
Is there a way to achieve this ?