I have the following page setup in App.js
StackNavigator({
MyTab: {
screen: TabNavigator({ }),
navigationOptions: { title: 'Header title' }
}
})
Bottom tab navigator with 4 screens in it: Home, Search, Card and Account. This tab navigator is inside stack navigator since i will be needing stack navigator to open other screens inside these pages.
In my case I don't need header in the Home page inside tab navigator. But according to this code every page inside tab navigator will have header. How can I achieve this?
Complete code flow is this.
App.js
import HomeScreen from './src/pages/HomeScreen';
import SearchScreen from './src/pages/SearchScreen';
import CartScreen from './src/pages/CartScreen';
import AccountScreen from './src/pages/AccountScreen';
const BottomTabNavigator = createBottomTabNavigator(
{
Home: {
screen: HomeScreen,
},
Search: {
screen: SearchScreen,
},
Cart: {
screen: CartScreen,
},
Account: {
screen: AccountScreen,
},
},
{
defaultNavigationOptions: ({navigation}) => ({
tabBarIcon: ({focused, tintColor}) => {
const {routeName} = navigation.state;
return (
<Image
style={styles.iconSize}
source={require('./src/assets/img/icon.png')}
/>
);
},
}),
tabBarPosition: 'bottom',
tabBarOptions: {
style: {
backgroundColor: '#fff',
},
showLabel: false,
indicatorStyle: {
backgroundColor: 'red',
},
},
animationEnabled: true,
swipeEnabled: false,
},
);
const HomeStackNavigator = createStackNavigator(
{
HomeStack: {
screen: BottomTabNavigator,
},
},
{
headerBackTitleVisible: false,
},
);
export default createAppContainer(HomeStackNavigator);
HomeScreen.js
import React, {Component} from 'react';
import {View, Text} from 'react-native';
export default class HomeScreen extends Component {
static navigationOptions = {
header: null,
};
render() {
return (
<View>
<Text> HomeScreen </Text>
</View>
);
}
}
Just add static navigationOptions in your code just anywhere in the code except inside any method like render or componentDidMount, it will work
export default class Home extends Component {
static navigationOptions = {
header: null
}
}
UPDATE:
Try to update your parent stack
const HomeStackNavigator = createStackNavigator(
{
HomeStack: {
screen: BottomTabNavigator,
},
},
{
headerBackTitleVisible: false,
headerMode: 'none',
},
);
Old answer
Try below
export default class Home extends React.Component {
static navigationOptions = () => {
const navigationOptions = {
header: null,
headerMode: 'none',
};
return navigationOptions;
};
}
Related
I have made my custom header and i want to remove react-native default header.
I have tried with
Setting option "header: null" in navigationOptions of
createBottomTabNavigator
header:null in HomeScreen.js file
but it's not working. Please help to solve this issue. Here is my navigation code. I am attaching screenshot exactly what i want to remove.
import React from "react";
import { Platform } from "react-native";
import { createStackNavigator } from "react-navigation-stack";
import { createBottomTabNavigator } from "react-navigation-tabs";
import TabBarIcon from "../components/TabBarIcon";
import HomeScreen from "../screens/HomeScreen";
import SavedScreen from "../screens/SavedScreen";
import BookingScreen from "../screens/BookingScreen";
import BeAHostScreen from "../screens/BeAHostScreen";
import ReferEarnScreen from "../screens/ReferEarnScreen";
import BookingInnerScreen from "../screens/BookingInnerScreen";
import { Icon } from "react-native-elements";
const config = Platform.select({
web: { headerMode: "screen" },
default: {}
});
const tabNavigator = createBottomTabNavigator({
Home: {
screen: HomeScreen,
defaultNavigationOptions: {
title: "App Name Here"
},
navigationOptions: {
tabBarLabel: "Home",
tabBarOptions: {
activeTintColor: "#00E8AC"
},
tabBarIcon: ({ focused }) => {
return focused ? (
<Icon name="md-home" type="ionicon" color="#00E8AC" />
) : (
<Icon name="md-home" type="ionicon" color="#ccc" />
);
}
}
},
Saved: {
screen: SavedScreen,
defaultNavigationOptions: {
title: "Saved"
},
navigationOptions: {
tabBarLabel: "Saved",
tabBarOptions: {
activeTintColor: "#00E8AC"
},
tabBarIcon: ({ focused }) => {
return focused ? (
<Icon name="md-heart" type="ionicon" color="#00E8AC" />
) : (
<Icon name="md-heart" type="ionicon" color="#ccc" />
);
}
}
}
});
const MyApp = createStackNavigator(
{
BookingInner: BookingInnerScreen,
Tabs: {
screen: tabNavigator
}
},
{
initialRouteName: "Tabs"
}
);
export default MyApp;
Try this :
const MyApp = createStackNavigator(
{
BookingInner: BookingInnerScreen,
Tabs: {
screen: tabNavigator
}
},
{
initialRouteName: "Tabs",
headerMode: 'none',
}
);
I want to create Drawer Navigation That contains Menu and Home Screen. I tried a lot but I am unable to resolve this issue and as per update of react native we have to wrap the navigation in CreateAppContainer, as you can see i wraped it but nothing is resolved.
"MainComponent"
import React, {Component} from 'react';
import { View, Platform } from 'react-native';
import Menu from './MenuComponent';
import Home from './HomeComponent';
import DishDetail from './DishDetailComponent';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation-stack';
const MenuNavigator = createStackNavigator({
Menu: { screen: Menu },
DishDetail: { screen: DishDetail }
},
{
initialRouteName: 'Menu',
navigationOptions: {
headerStyle: {
backgroundColor: "#512DA8"
},
headerTintColor: '#fff',
headerTitleStyle: {
color: "#fff"
}
}
}
);
const HomeNavigator = createStackNavigator({
Home: { screen: Home }
}, {
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: "#512DA8"
},
headerTitleStyle: {
color: "#fff"
},
headerTintColor: "#fff"
})
});
//-----------Main---------//
const MainNavigator = createDrawerNavigator({
Home:
{ screen: HomeNavigator,
navigationOptions: {
title: 'Home',
drawerLabel: 'Home'
}
},
Menu:
{ screen: MenuNavigator,
navigationOptions: {
title: 'Menu',
drawerLabel: 'Menu'
},
}
}, {
drawerBackgroundColor: '#D1C4E9'
});
class Main extends Component() {
render() {
return(
<View>
<MainNavigator />
</View>
);
}
}
export default createAppContainer(Main);
////////////////////App.js/////////////////
import React from 'react';
import Main from './components/MainComponent';
export default class App extends React.Component {
render() {
return (
<Main />
);
}
}
I think the issue is i am not wrapping the appContainer Correctly.
You are importing wrong. use like
import { createDrawerNavigator } from 'react-navigation-drawer';
import { createStackNavigator } from 'react-navigation-stack';
instead of
import { createStackNavigator, createDrawerNavigator } from 'react-navigation-stack';
Try this way
const LoginNavigator = createStackNavigator({
Login: {
screen: Login,
navigationOptions: {
header: null,
}
},
SignUp: {
screen: SignUp,
navigationOptions: {
header: null,
}
}
});
class NavigationDrawerStructure extends Component {
toggleDrawer = () => {
//Props to open/close the drawer
this.props.navigationProps.toggleDrawer();
};
render() {
return (
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
<Image
source={require('../Img/hamburger.png')}
style={{ width: 25, height: 25, marginLeft: 20, tintColor: '#ffffff' }}
/>
</TouchableOpacity>
</View>
);
}
}
const HomeActivity_StackNavigator = createStackNavigator({
Home: {
screen: Main,
navigationOptions: ({ navigation }) => ({
title: 'Dashboard',
headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
headerStyle: {
backgroundColor: '#FF9800'
},
headerTintColor: '#fff'
}),
},
}, {headerLayoutPreset: 'center'});
const DrawerNavigators = createDrawerNavigator({
//Drawer Optons and indexing
Main: {
screen: HomeActivity_StackNavigator,
navigationOptions: {
drawerLabel: 'Dashboard',
}
}
});
const NonDrawerNavigators = createStackNavigator({
TaskView: {
screen: TaskView,
navigationOptions: {
title: 'Task',
headerStyle: {
backgroundColor: '#FF9800',
},
headerTintColor: '#fff'
}
},
TeamView: {
screen: TeamView,
navigationOptions: {
title: 'Team',
headerStyle: {
backgroundColor: '#FF9800',
},
headerTintColor: '#fff'
}
}
}, {headerLayoutPreset: 'center'});
const AppNavigator = createStackNavigator({
drawerStack: {
screen: DrawerNavigators,
navigationOptions: {
header: null
}
},
nonDrawerStack: {
screen: NonDrawerNavigators,
navigationOptions: {
header: null
}
}
});
export default createAppContainer(createSwitchNavigator({
SplashScreen: SplashScreen,
loginStack: LoginNavigator,
homeStack: AppNavigator
}, {
initialRouteName: 'SplashScreen'
})
);
I had modified your code as under. I hope this will help you.
"MainComponent"
import React, {Component} from 'react';
import { View, Platform } from 'react-native';
import Menu from './MenuComponent';
import Home from './HomeComponent';
import DishDetail from './DishDetailComponent';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation-stack';
const MenuNavigator = createAppContainer(createDrawerNavigator(
{
Menu: { screen: Menu },
DishDetail: { screen: DishDetail }
},
{
contentComponent: SideMenu,
drawerWidth: normalize(280),
initialRouteName: "Menu"
}
));
const HomeNavigator = createAppContainer(createStackNavigator(
{
Home: { screen: Home }
},
{
// Default config for all screens
headerMode: 'none',
initialRouteName: 'Home',
// transitionConfig: noTransitionConfig,
},
));
class Main extends Component() {
render() {
return(
<View>
<MainNavigator />
</View>
);
}
}
export default createAppContainer(Main);
////////////////////App.js/////////////////
import React from 'react';
import Main from './components/MainComponent';
export default class App extends React.Component {
render() {
return (
<Main />
);
}
}
As it says in the title, contentComponent not working. I cant quite figure out why it is not rendering the content in contentcomponent.
It shows the screens 1 2 and logout that I have in the drawer navigator instead of it. Any suggestions is appreciated?
Routes.js
import React from 'react';
import {
createAppContainer,
createSwitchNavigator,
createStackNavigator,
createBottomTabNavigator,
createDrawerNavigator
} from 'react-navigation';
import { SideNavigation } from './SideNavigation';
export const SignedOut = createStackNavigator(
{
Login: {
screen: LoginForm,
navigationOptions: {
header: null
}
},
Register: {
screen: RegisterForm
},
VerifyPhone: {
screen: PhoneVerify,
navigationOptions: {
headerLeft: null
}
}
},
{
initialRouteName: "Login"
}
);
export const Tabs = createBottomTabNavigator({
Main: {
screen: createStackNavigator({
Main: Main
}),
},
Events: {
screen: createStackNavigator({
Events: Events
}),
},
Shop: {
screen: createStackNavigator({
Shop: Shop
}),
},
Profile: {
screen: createStackNavigator({
Profile: Profile
}),
},
});
export const Stack = createStackNavigator(
{
Drawer: {
screen: SideNavigation,
navigationOptions: {
header: null,
},
},
DefaultScreen: {
screen: Main,
}
}
);
export const createRootNavigator = (loggedin) => {
return createAppContainer(createSwitchNavigator(
{
SignedIn: {
screen: Stack
},
SignedOut: {
screen: SignedOut
},
},
{
//initialRouteName: loggedin ? "SignedIn" : "SignedOut"
initialRouteName: "SignedIn"
}
));
};
SideNavigation.js
import { createDrawerNavigator, SafeAreaView } from 'react-navigation';
import { ScrollView } from 'react-native-gesture-handler';
import { Text, Dimensions } from 'react-native';
import { Stack } from "./Routes";
export const SideNavigation = createDrawerNavigator(
{
home: Stack
},
{
contentComponent: (props) => {
return <Drawer {...props} >
<ScrollView>
<SafeAreaView
forceInset={{top: 'always', horizontal: 'never'}}
>
<Text
onPress={() => {
props.navigation.navigate('Screen1');
props.navigation.closeDrawer();
}}
>
Testing Side 1
</Text>
<Text
onPress={() => {
props.navigation.navigate('Screen2');
props.navigation.closeDrawer();
}}
>
Testing side 2
</Text>
</SafeAreaView>
</ScrollView>
</Drawer>
},
}
);
Edit: Code has been updated. i am now getting an error that says 'home'must be a React component.
give your custom component as a separated component file, like this:
import Drawer from "./somewhere";
const Stack = createStackNavigator(
{
DefaultScreen: {
screen: Main,
}
}
);
const DrawerNavigator = createDrawerNavigator({
home: Stack <<<<this is an address of the stack navigator you should created above
}, {
contentComponent: (props) => {
return <Drawer {...props} /> <<<<Drawer
},
drawerPosition: 'right',
drawerType: 'slide',
drawerWidth: width * 0.8
}
)
more logical and cleaner... but don't forget to give drawer to your app container:
return createAppContainer(createSwitchNavigator(
{
SignedIn: {
screen: DrawerNavigato//not stack
},
SignedOut: {
screen: SignedOut
},
},
{
//initialRouteName: loggedin ? "SignedIn" : "SignedOut"
initialRouteName: "SignedIn"
}
));
I have one file with my whole navigation stack. In my navigation header I have a menu and I want to open a Drawer. Now In this example I get the error: Cannot read property 'navigation' of undefined
My AppNavigation file:
import React from 'react';
import { Text } from 'react-native';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation';
import Login from '../components/Login';
import Dashboard from '../components/Dashboard';
import NewNotification from '../components/NewNotification';
const GuestStack = createStackNavigator(
{
loginScreen: { screen: Login },
}, {
headerMode: 'float',
headerLayoutPreset: 'center',
navigationOptions: {
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welcome',
headerTintColor: 'black',
},
},
);
const LoggedinStack = createDrawerNavigator({
dashboard: { screen: Dashboard },
newNotifciation: { screen: NewNotification },
});
const LoggedinNavigation = createStackNavigator(
{
LoggedinStack: { screen: LoggedinStack },
}, {
headerMode: 'float',
navigationOptions: {
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welkom!',
headerTintColor: 'black',
headerLeft: <Text onPress = { () =>
this.props.navigation.openDrawer('dashboard')
// navigation.openDrawer('dashboard')
}>Menu</Text>,
},
},
);
const VveNavigator = createStackNavigator(
{
guestStack: {
screen: GuestStack,
},
loggedinStack: {
screen: LoggedinNavigation,
},
}, {
headerMode: 'none',
initialRouteName: 'guestStack',
},
);
export default AppNavigator;
The problem seems to be over here:
headerLeft: <Text onPress = { () =>
this.props.navigation.openDrawer('dashboard')
// navigation.openDrawer('dashboard')
}>Menu</Text>,
And then in my App.js I have
export default class App extends React.Component {
render() {
return (
<APPNavigator />
);
}
}
Version of react navigation is 2.18.1
Thanks
headerLeft doesn't receive navigation prop (check the source code). So if you'd like to use a navigation prop on press, you should consider to refactor your stack navigator config:
const LoggedinNavigation = createStackNavigator(
{
LoggedinStack: { screen: LoggedinStack },
}, {
headerMode: 'float',
navigationOptions: ({ navigation }) => ({ // here you get the navigation
headerStyle: { backgroundColor: '#61b1cd' },
title: 'Welkom!',
headerTintColor: 'black',
headerLeft: (
<Text
onPress={() => {
navigation.openDrawer()
}}
>
Menu
</Text>
),
}),
},
);
Check this issue for more options.
I'm trying to navigate between screens in React Native, by using React Navigation.
Currently, I have the following. Note that EmployeeStack is nested inside of RootStack.
RootStack.js:
import LoginForm from '../components/LoginForm';
import EmployeeStack from './EmployeeStack';
import { StackNavigator } from 'react-navigation';
const routes = {
Home: {screen: LoginForm},
EmployeeList: {screen: EmployeeStack}
};
const options = {
initialRouteName: 'Home'
};
const RootStack = StackNavigator(routes, options);
export default RootStack;
EmployeeStack.js:
import EmployeeList from '../components/EmployeeList';
import AnotherScreen from '../components/AnotherScreen';
import { StackNavigator } from 'react-navigation';
const routes = {
EmployeeList: {screen: EmployeeList},
AnotherScreen: {screen: AnotherScreen},
};
const options = {
initialRouteName: 'EmployeeList'
};
const EmployeeStack = StackNavigator(routes, options);
export default EmployeeStack;
In EmployeeList.js, I have a button that does this:
<Button
onPress={() => this.props.navigation.navigate('AnotherScreen')}
title="Go123"
color="#841584"
/>
This button does not navigate to AnotherScreen when pressed. Interestingly though, if I change the navigate argument to 'EmployeeList' or 'Home', it navigates to those screens properly.
How can I navigate to AnotherScreen?
In order to navigate between screens from login -> home screen you need to define separate navigation. Use primary navigation as common for other navigations
import HomeScreen from "../screens/user/Home";
import LoginScreen from "../screens/Login";
import ContactScreen from "../screens/Contact";
import LogoutScreen from "../screens/Logout";
........
const LoginStack = StackNavigator({
loginScreen: { screen: LoginScreen },
// signupScreen: { screen: SignupScreen },
// forgottenPasswordScreen: { screen: ForgottenPasswordScreen, navigationOptions: { title: 'Forgot Password' } }
}, {
headerMode: 'none',
navigationOptions: {
headerVisible: false,
gesturesEnabled: false,
}
})
const HomeNavigation = StackNavigator({
HomeStack: { screen: HomeStack }
}, {
headerMode: 'float',
navigationOptions: ({navigation}) => ({
gesturesEnabled: false,
headerStyle: {backgroundColor: '#29B1FC'},
headerTintColor: '#ffffff',
}),
})
const HomeStack = DrawerNavigator({
HomeScreen: { screen: HomeScreen },
ContactScreen: { screen: ContactScreen },
LogoutScreen: { screen: LogoutScreen },
},
{
contentOptions: {
activeTintColor: 'black',
activeBackgroundColor : 'transparent',
inactiveTintColor : 'black',
itemsContainerStyle: {
marginVertical: 0,
},
iconContainerStyle: {
opacity: 1,
},
itemStyle :{
height : 50,
}
},
}
)
const PrimaryNav = StackNavigator({
loginStack: { screen: LoginStack },
HomeStack: { screen: HomeNavigation },
}, {
// Default config for all screens
headerMode: 'none',
title: 'Main',
initialRouteName: 'loginStack'
})
export default PrimaryNav
........
And your button press will work like this
<Button
onPress={() => this.props.navigation.navigate('HomeStack')}
//HomeStack = navigation of PrimaryNav
title="Go123"
color="#841584"
/>