Rendering both drawer and tabbed navigation with react navigation - reactjs

I'm using React Navigation in my react native app, and I'm looking to render both the drawer navigation and tab navigation components simultaneously.
At first, I tried to render both in the root app component, but that threw an error that appears in their documentation here. I then attempted the solution listed below, which produced the following for me:
const TabNavigator = createBottomTabNavigator({
Settings: {
screen: SettingsScreen
}
});
const AppNavigator = createDrawerNavigator({
Home: {
screen: HomeScreen
},
Products: {
screen: ProductsScreen
},
TabNav: TabNavigator
});
export default class App extends React.Component {
render() {
return <AppNavigator />;
}
}
The tab navigation only shows when I select the TabNav link in the drawer navigation. I want it on every screen. I also don't want the TabNav label to show in the drawer navigation.
Am I missing something in the documentation, or is this the intended functionality?

For the first question, move the TabNav to first in the navigator like this
TabNav: TabNavigator,
Home: {
screen: HomeScreen
},
Products: {
screen: ProductsScreen
}
or else you change the initialRouteName to "TabNav"
For the Second question,
If you want the TabNavigation to show inside the HomeScreen and ProductsScreen you need to include TabNavigation in both
or they need to have a parent TabNavigation which contains those. I can add a sample code if you post what the TabNav contains.
And for the Third question,
You can use contentComponent in DrawerNavigator example like this
contentComponent: props => (
<AppDrawerContent {...props} navigation={props.navigation} />
),
Edited:
If you want TabNavigator to show on each one. I think you should change the way you Structure your navigator.
export default class App extends React.Component {
render() {
return < TabNavigator />;
}
}
const TabNavigator = createBottomTabNavigator({
Drawer: {
screen: AppNavigator
}
});
const AppNavigator = createDrawerNavigator({
Home: {
screen: HomeScreen
},
Products: {
screen: ProductsScreen
}
});
or else if you want TabNavigator inside each screen individually then
const AppNavigator = createDrawerNavigator({
HomeNavigator: {
screen: HomeScreenNavigator
},
Products: {
screen: ProductsScreen
}
});
const HomeScreenNavigator = createBottomTabNavigator({
Home: {
screen: HomeScreen
},
Settings: {
screen: SettingsScreen
}
});
export default class App extends React.Component {
render() {
return <AppNavigator />;
}
}
Like wise for Products screen also.
What you have actually done is you included the TabNav as Drawer Screen so it does appear in the Drawer side bar.
Even if this structure doesn't work you need to check on restructuring it. Or you may give a images how you want to show it. May be I could help you with better understanding.

Related

React Native | Navigator talking back to Initial Page

I have this navigator Setup on my Routes Page and I am expecting that when I press this.props.navigation.goback function it should take me to the second page if I am on the third page but apparently it is taking me to the first page.
Even with the natural goback Behavior of Android or IOS, it's taking me to the First Page.
I navigate Via
this.props.navigation.navigate('Screens')
NavigatorPage: Here I am using DrawerNavigator
import { createAppContainer ,createDrawerNavigator} from 'react-navigation';
import Dashboard from './pages/dashboard';
import Splash from './pages/Splash';
import Readmore from './pages/readmore';
import Tabs from './pages/tab';
import Check from './pages/checking';
import Search from './pages/searchpage';
import SideMenu from './componenet/sideMenu'
const AppNavigator = createDrawerNavigator({
// Prac: { screen: Check},
Splash :{ screen: Splash},
Dashboard: { screen: Dashboard},
Readmore: { screen: Readmore },
Tabs: { screen: Tabs },
Search: { screen: Search }
},
{
contentComponent: SideMenu,
drawerWidth: 300,
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
});
export default createAppContainer(AppNavigator);
Edit ONE After doing this
const AppNavigator = createDrawerNavigator({
// Prac: { screen: Check},
stacknav:{screen:stacknav}
},
{
contentComponent: SideMenu,
drawerWidth: 300,
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
});
const stacknav = createStackNavigator ({ Splash :{ screen: Splash},
Dashboard: { screen: Dashboard},
Readmore: { screen: Readmore },
Tabs: { screen: Tabs },
Search: { screen: Search } });
export default createAppContainer(AppNavigator);
Getting an Error of this
The answer is simple
Put all the screens of your drawernavigator inside a new createstacknavigator.
Then inherit stack navigator inside drawernavigator as a child.
I.e
const stacknav = createStackNavigator ({ Splash :{ screen: Splash},
Dashboard: { screen: Dashboard},
Readmore: { screen: Readmore },
Tabs: { screen: Tabs },
Search: { screen: Search } });
Const appNavigator = createdrawernavigator({ stacknav:{screen:stacknav}},{ ...}}
Hope it helps. Sorry for poor code structure as i am replying from a mobile device. But surely it ll be of your help. Thanks !
Use createStackNavigator instead of createDrawerNavigator.
I have found the solution and before that, there are some things that we need to watch.
Make sure every const Name start with Capital Letter
I couldn't just simply import the stack navigator to the drawer navigator from the same file.
So I put the stack into another file and export default it directly into my Drawernavigator.
And that fixed my issue

react native - how to use stackNavigator and DrawerNavigator together

I am trying to use react native DrawerNavigator and StackNavigator together but for some reason I can't make it work.
Below is my code:
import {DrawerNavigator,StackNavigator, } from 'react-navigation'
class App extends Component {
render(){
return(
<View style={{flex:1,backgroundColor:'transparent'}}>
<AppStackNavigator/>
<AppDrawerNavigator/>
</View>
)
}
}
const AppDrawerNavigator = DrawerNavigator({
Home:HomeScreen,
Price:PriceScreen,
Info:InfoScreen,
Login:LoginScreen
})
const AppStackNavigator = StackNavigator({
Home:HomeScreen,
Price:PriceScreen,
Info:InfoScreen,
Login:LoginScreen
})
export default App
When I run this code my app screen is split into half, top have shows stackNavigator screen and the bottom half shows DrawerNavigator screen.
Is there a way I can make these two work together?
Also, what is the difference between stackNavigator and createStack Navigator? I don't see the diffence when I run these.
You need to nest the Navigators to get the result you are asking for.
For example if you want a stack navigation inside one of the screens in your drawer navigation you can do this:
import {DrawerNavigator, StackNavigator} from 'react-navigation'
class App extends Component {
render() {
return (
<View style={{ flex: 1, backgroundColor: 'transparent' }}>
<AppDrawerNavigator />
</View>
)
}
}
const AppStackNavigator = StackNavigator({
Home: HomeScreen,
Price: PriceScreen,
Info: InfoScreen,
Login: LoginScreen
})
const AppDrawerNavigator = DrawerNavigator({
Home: AppStackNavigator,
// Price: PriceScreen,
// Info: InfoScreen,
// Login: LoginScreen
})
export default App
The important part is
const AppDrawerNavigator = DrawerNavigator({
Home: AppStackNavigator, <--- This
// Price: PriceScreen,
// Info: InfoScreen,
// Login: LoginScreen
})
Now the "Home" Screen in your Drawer Navigation has the ability to use the stack navigation. You can do this for the others screens as well.

React Navigation V2 Hide Single Tab From Bottom Tab Navigator

I am working on a React Native app. When the user loads the app, I want the first screen they see to be a Home screen to tell them about some of the features of the app. Currently, I am using a BottomTabNavigator with nested StackNavigators inside. I have been able to get my Home screen added, but it always shows up the in TabNavigator. Is there a way to make a single tab item not present in any of the screens?
PS. I have tried using a StackNavigator as my parent like the following, but my TabNavigator does not show up:
const AppNavigator = createBottomTabNavigator({
Item1: Item1,
Item2: Item2,
Item3: Item3,
Item4: Item4,
Item5: Item5
});
const MainNavigator = createStackNavigator({
Home: Home,
Content: AppNavigator
}
Any ideas?
UPDATE
If I have screens called Item1, Item2, Item3, Item4, Item5, and Home, I want to make it display Home first. In addition, I want the TabBar visible at the bottom, but it can't include the Home screen.
You likely want to nest your main app tab navigator inside another tab navigator like this:
const AppNavigator = TabNavigator({
Main: { screen: MainScreen },
Settings: { screen: SettingsScreen },
});
export default TabNavigator(
{
Home: { screen: HomeScreen },
App: { screen: AppNavigator },
},
{
navigationOptions: ({ navigation }) => ({
tabBarVisible: false,
}),
}
);
I think SwitchNavigator is what do you need https://reactnavigation.org/docs/en/switch-navigator.html. Try like this
const MainNavigator = createSwitchNavigator({
Home: Home,
Content: AppNavigator
}
If you want TabNavigator you can simply do the same
const MainNavigator = createBottomTabNavigator({
Home: Home,
Content: AppNavigator
}
Create a custom tabBarComponent for your Tab Navigator to use.
Map through the routes in that custom tabBarComponent and don't render anything for that Tab if route.routeName === 'Home'.
const TabBar = props => {
return (
{navigation.state.routes.map((route, i) => {
if (route.routeName !== 'Home') {
return (
<Tab...

React Navigation confusion

I started learning React Native, I know how to create TabNavigator or DrawerNavigator but I need access to props.navigation on landing page where I don't have TabNavigation, how I'am supposed to get it.
I tried this:
const App = createStackNavigator({
LandingScreen: { screen: LandingScreen },
Register: { screen: RegisterScreen },
Login: { screen: LoginScreen}});
But still, this.props are empty object.
And what is stackNavigator in plain language, does it just define navigation in order to be able to use navigation ?
In your LandingScreen you can able to get the this.props.navigation. You can show your TabNavigation as your initial route. You can even set Stack Navigation for each Tab. For React Navigation(v2) Documentation https://reactnavigation.org/docs/en/tab-based-navigation.html
Stack Navigation:
Provides a way for your app to transition between screens where each new screen is placed on top of a stack.
//Stack Navigator
const App = createStackNavigator({
Home: { screen: Tabs }, //You can nest your TabNavigator here, Hence the LandingScreen inside your HomeStack will shown as your Initial screen
Register: { screen: RegisterScreen },
Login: { screen: LoginScreen}});
//Sample Tab Navigator
const Tabs = createBottomTabNavigator(
{
Home: HomeStack,
Settings: SettingsStack,
},
{
/* Other configuration remains unchanged */
}
);
const HomeStack = createStackNavigator({
LandingScreen: LandingScreen,
Details: DetailsScreen,
});
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
Details: DetailsScreen,
});
You can get your navigation Prop by using below
//LandingScreen Component
export default LandingScreen extends Component{
static navigationOptions = ({navigation}) => ({ //you can able to destructure your navigation prop here
headerTitle: 'Langing Page'
})
render(){
return(
//your logic here
)
}
}

NewCard screen from DrawerNavigator item

I hope i'm able to explain this properly, and that there isn't already an answer out there. It seems there is still a few design decisions up in the air for apps that have a StackNavigator nested inside DrawerNavigator.
What I'm trying to achieve: I have a link to a "Settings" screen in my DrawerNavigator, similar to a lot of apps. I will use Google Play Music as an example for what I want. Clicking on "Settings" sends you to a new screen with only a "back" / "done" button. The Drawer Menu is not accessible.
Question: How can I add a link in the DrawerNavigator that links to a new card/modal view? I'm guessing it can be achieved my some nested navigator stack, but I haven't been able to get anything that works.
Sample code:
const DashboardNavigator = StackNavigator({
Home: { screen: HomeScreen }
})
const SettingsNavigator = StackNavigator({
Settings: {screen: SettingsScreen}
// I thought adding 'mode': 'modal would give me the functionality
// I'm looking for my it doesn't
}, { mode: 'modal', initialRoute: 'Settings' })
const DrawerNavigation = DrawerNavigator({
Home: {
screen: DashboardNavigator
},
Settings: {
screen: SettingsNavigator
}
})
There is a pull request to allow disabling the Drawer Menu on specific screens so i'm not really worried about that right now, but just navigating to a screen where navigation.goBack() takes me back to the last screen I was on (with the card slide animation).
Was able to get it with this, although I still can access the Drawer menu... Hopefully they add the ability to disable the Drawer soon.
const DrawerComponent = ({ navigation }) => {
return (
<View>
<Button onPress={() => navigation.navigate("SettingsView")} title="settings" />
</View>
)
}
const DashboardNavigator = StackNavigator({
Home: { screen: HomeScreen },
SettingsView: { screen: SettingsScreen }
})
const DrawerNavigation = DrawerNavigator({
Home: {
screen: DashboardNavigator
}
}, { contentComponent: DrawerComponent })
const RootNavigator = StackNavigator({
Root: {
screen: DrawerNavigation
}
})

Resources