I am using react-navigation
in React Native
and I want to create a sidebar menu which opens as an overlay that es from left to the right and fills up around 80-90% of the width.
Without react-navigation, this is possible with packages such as react-native-side-bar
, but I am wondering if I can get the exact same feature with DrawerNavigator.
But it seems DrawerNavigator has menu buttons. Isn't it possible to configure the overlay myself?
Edit
Solution 1
One way would be to use
const MyStackNavigator = StackNavigator({
A: { screen: AScreen },
B: { screen: BScreen },
C: { screen: CScreen }
});
const RootNavigator = DrawerNavigator({
A: { screen: MyStackNavigator },
}, {
// set content of side bar
contentComponent: (props) => <Sidebar />
});
but then it will possible to drag in the drawer from all screens AScreen
, BScreen
, and CScreen
, while I only want it to be there for AScreen
, since the StackNavigator is nested in the DrawerNavigator.
Solution 2
Another solution would be to use
const MyDrawerNavigator = DrawerNavigator({
A: { screen: AScreen },
}, {
// set content of side bar
contentComponent: (props) => <Sidebar />
});
const RootNavigator = StackNavigator({
A: { screen: MyDrawerNavigator },
B: { screen: BScreen },
C: { screen: CScreen }
});
but then the header of AScreen
will be on top since the DrawerNavigator is nested in A
.
I am using react-navigation
in React Native
and I want to create a sidebar menu which opens as an overlay that es from left to the right and fills up around 80-90% of the width.
Without react-navigation, this is possible with packages such as react-native-side-bar
, but I am wondering if I can get the exact same feature with DrawerNavigator.
But it seems DrawerNavigator has menu buttons. Isn't it possible to configure the overlay myself?
Edit
Solution 1
One way would be to use
const MyStackNavigator = StackNavigator({
A: { screen: AScreen },
B: { screen: BScreen },
C: { screen: CScreen }
});
const RootNavigator = DrawerNavigator({
A: { screen: MyStackNavigator },
}, {
// set content of side bar
contentComponent: (props) => <Sidebar />
});
but then it will possible to drag in the drawer from all screens AScreen
, BScreen
, and CScreen
, while I only want it to be there for AScreen
, since the StackNavigator is nested in the DrawerNavigator.
Solution 2
Another solution would be to use
const MyDrawerNavigator = DrawerNavigator({
A: { screen: AScreen },
}, {
// set content of side bar
contentComponent: (props) => <Sidebar />
});
const RootNavigator = StackNavigator({
A: { screen: MyDrawerNavigator },
B: { screen: BScreen },
C: { screen: CScreen }
});
but then the header of AScreen
will be on top since the DrawerNavigator is nested in A
.
-
How is what you're trying to do different than what the DrawerNavigator does by default? Looking at the example for
react-native-side-bar
they're doing the same thing. – Spencer Carli Commented May 23, 2017 at 0:49 - It is the exact same. The problem is how to organize the navigators. I have updated my question with an example – Jamgreen Commented May 24, 2017 at 9:08
1 Answer
Reset to default 5I needed the same functionality that you're describing and managed to get it working with React navigation. Basically, I needed a fully custom drawer (side-menu).
This is my navigator setup:
const MainNavigator = DrawerNavigator({
Home: {
screen: StackNavigator({
Search: {
screen: SearchScreen
},
Result: {
screen: ResultScreen
}
})
},
Saved: {
screen: StackNavigator({
SavedStack: {
screen: SavedWordsScreen
}
})
},
About: {
screen: StackNavigator({
AboutStack: {
screen: AboutScreen
}
})
}
},{
contentComponent: props => (<Drawer navigation={props.navigation} drawerProps={{...props}} />)
});
As you see, I've created a Drawer ponent which contains my custom drawer content. This is the basic setup of that ponent:
import React, { Component } from 'react';
import { ScrollView, View, Text } from 'react-native';
import { Button } from 'react-native-elements';
class Drawer extends Component {
render() {
return (
<View>
<Button title="Search" onPress={() => this.props.navigation.navigate('Home')} />
<Button title="Saved" onPress={() => this.props.navigation.navigate('Saved')} />
<Button title="About" onPress={() => this.props.navigation.navigate('About')} />
</View>
);
}
}
I hope this helps. I've simplified some of the code to mainly show the relevant bits so if you have any follow-up questions, just ask!