React-native how to implement drawer and tabBar in stack navigator
I always use react-native-router-flux for navigation, but on this project I need to use react-navigation and I got some troubles with this. I need to implement
drawer and tabBar inside stack navigator.
Problems:
- I use header component from native-base library but i can't open
drawer. - How to use my own customize component for drawer and tabBar?
- Maybe I need to chage structure. I will consider any recommendations how to improve structure.
I used version 3 of react navigation.
My code:
const AppStackNavigator = createStackNavigator({
loginFlow: {
screen: createStackNavigator({
intro: { screen: Intro },
login: { screen: Login },
registration: { screen: Registration },
}),
navigationOptions: {
header: null
}
},
mainFlow: {
screen: createStackNavigator({
MyDrawer: createDrawerNavigator({
Dashboard: {
screen: Home,
},
first: {
screen: first,
},
second: {
screen: second
},
third: {
screen: third
},
last: {
screen: last
}
}),
// settings: { screen: SettingsScreen },
someTab: {
screen: createBottomTabNavigator({
main: { screen: Home },
firsrTab: { screen: Screen1 },
secondTab: { screen: Screen2 },
thirdTab: { screen: Screen3 },
nextTab: { screen: Screen4 }
}),
navigationOptions: {
header: null
},
}
}),
navigationOptions: {
header: null
}
}
});
const AppContainer = createAppContainer(AppStackNavigator);
import React from 'react';
import { Header, Left, Icon, Right } from 'native-base';
const CustomHeader = (props) => {
return(
<Header>
<Left>
<Icon
name='menu'
onPress={() => {this.props.navigation.openDrawer()}}
/>
</Left>
</Header>
)
}
export { CustomHeader }
react-native react-navigation native-base
add a comment |
I always use react-native-router-flux for navigation, but on this project I need to use react-navigation and I got some troubles with this. I need to implement
drawer and tabBar inside stack navigator.
Problems:
- I use header component from native-base library but i can't open
drawer. - How to use my own customize component for drawer and tabBar?
- Maybe I need to chage structure. I will consider any recommendations how to improve structure.
I used version 3 of react navigation.
My code:
const AppStackNavigator = createStackNavigator({
loginFlow: {
screen: createStackNavigator({
intro: { screen: Intro },
login: { screen: Login },
registration: { screen: Registration },
}),
navigationOptions: {
header: null
}
},
mainFlow: {
screen: createStackNavigator({
MyDrawer: createDrawerNavigator({
Dashboard: {
screen: Home,
},
first: {
screen: first,
},
second: {
screen: second
},
third: {
screen: third
},
last: {
screen: last
}
}),
// settings: { screen: SettingsScreen },
someTab: {
screen: createBottomTabNavigator({
main: { screen: Home },
firsrTab: { screen: Screen1 },
secondTab: { screen: Screen2 },
thirdTab: { screen: Screen3 },
nextTab: { screen: Screen4 }
}),
navigationOptions: {
header: null
},
}
}),
navigationOptions: {
header: null
}
}
});
const AppContainer = createAppContainer(AppStackNavigator);
import React from 'react';
import { Header, Left, Icon, Right } from 'native-base';
const CustomHeader = (props) => {
return(
<Header>
<Left>
<Icon
name='menu'
onPress={() => {this.props.navigation.openDrawer()}}
/>
</Left>
</Header>
)
}
export { CustomHeader }
react-native react-navigation native-base
add a comment |
I always use react-native-router-flux for navigation, but on this project I need to use react-navigation and I got some troubles with this. I need to implement
drawer and tabBar inside stack navigator.
Problems:
- I use header component from native-base library but i can't open
drawer. - How to use my own customize component for drawer and tabBar?
- Maybe I need to chage structure. I will consider any recommendations how to improve structure.
I used version 3 of react navigation.
My code:
const AppStackNavigator = createStackNavigator({
loginFlow: {
screen: createStackNavigator({
intro: { screen: Intro },
login: { screen: Login },
registration: { screen: Registration },
}),
navigationOptions: {
header: null
}
},
mainFlow: {
screen: createStackNavigator({
MyDrawer: createDrawerNavigator({
Dashboard: {
screen: Home,
},
first: {
screen: first,
},
second: {
screen: second
},
third: {
screen: third
},
last: {
screen: last
}
}),
// settings: { screen: SettingsScreen },
someTab: {
screen: createBottomTabNavigator({
main: { screen: Home },
firsrTab: { screen: Screen1 },
secondTab: { screen: Screen2 },
thirdTab: { screen: Screen3 },
nextTab: { screen: Screen4 }
}),
navigationOptions: {
header: null
},
}
}),
navigationOptions: {
header: null
}
}
});
const AppContainer = createAppContainer(AppStackNavigator);
import React from 'react';
import { Header, Left, Icon, Right } from 'native-base';
const CustomHeader = (props) => {
return(
<Header>
<Left>
<Icon
name='menu'
onPress={() => {this.props.navigation.openDrawer()}}
/>
</Left>
</Header>
)
}
export { CustomHeader }
react-native react-navigation native-base
I always use react-native-router-flux for navigation, but on this project I need to use react-navigation and I got some troubles with this. I need to implement
drawer and tabBar inside stack navigator.
Problems:
- I use header component from native-base library but i can't open
drawer. - How to use my own customize component for drawer and tabBar?
- Maybe I need to chage structure. I will consider any recommendations how to improve structure.
I used version 3 of react navigation.
My code:
const AppStackNavigator = createStackNavigator({
loginFlow: {
screen: createStackNavigator({
intro: { screen: Intro },
login: { screen: Login },
registration: { screen: Registration },
}),
navigationOptions: {
header: null
}
},
mainFlow: {
screen: createStackNavigator({
MyDrawer: createDrawerNavigator({
Dashboard: {
screen: Home,
},
first: {
screen: first,
},
second: {
screen: second
},
third: {
screen: third
},
last: {
screen: last
}
}),
// settings: { screen: SettingsScreen },
someTab: {
screen: createBottomTabNavigator({
main: { screen: Home },
firsrTab: { screen: Screen1 },
secondTab: { screen: Screen2 },
thirdTab: { screen: Screen3 },
nextTab: { screen: Screen4 }
}),
navigationOptions: {
header: null
},
}
}),
navigationOptions: {
header: null
}
}
});
const AppContainer = createAppContainer(AppStackNavigator);
import React from 'react';
import { Header, Left, Icon, Right } from 'native-base';
const CustomHeader = (props) => {
return(
<Header>
<Left>
<Icon
name='menu'
onPress={() => {this.props.navigation.openDrawer()}}
/>
</Left>
</Header>
)
}
export { CustomHeader }
react-native react-navigation native-base
react-native react-navigation native-base
edited Jan 11 at 11:48
Raduan Santos
7691739
7691739
asked Nov 25 '18 at 22:09
HannibalHannibal
226
226
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You might wanna consider the SwitchNavigator
for the authentication flow instead of a Stack at the top
as it replaces the routes so that you can never navigate back to the login/signup/splash once you get into the application and for accessing Tabs and Drawer inside stack/switch, you can wrap the Drawer inside your top level navigator and tab inside the drawer.
So you root navigation would look like this.
export default RootNavigation = createSwitchNavigator({
LoginScreen: {screen: LoginContainer},
Application: {screen: AppDrawer},
});
Your drawer navigator should be like the following:
const AppDrawer = createDrawerNavigator({
ApplicationTab: {screen: TabBar},
... other screen that you might want to use in drawer navigation.
}, {
contentComponent : (props) => <MyCustomDrawer {...props} />
});
and, Tab Navigator would be,
const TabBar = createBottomTabNavigator({
TabScreen1: {screen: Tab1},
... other tabs...
}, {
tabBarComponent : (props) => <MyTabBar {...props} />
});
If you put each of those navigators in single file then please do declare Tab before Drawer and Drawer before the Switch, else it would give errors.
In my experience, customising drawer navigator is very simple and fruitful but customising tab is not, there aren't proper API doc for the same and community answers are also somewhat misleading.
BUT, with normal use cases and for most of the vivid ones too, you can do your job without needing to override the default one as it is already highly operable and customisable in terms of icons, materialism and each tab exposes its on onPress
that can also be easily overriden.
and as you as the drawer is not getting operated from/via the header, then can you please ensure that the navigation
prop you are using to operate the drawer open
close
or toggle
action is the one given by drawer ?
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have usedthis.props.navigation.openDrawer()
from the CustomHeader, you should useprops.navigation.openDrawer()
and also ensure that you are passing the props to yourCustomHeader
correctly.
– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472505%2freact-native-how-to-implement-drawer-and-tabbar-in-stack-navigator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might wanna consider the SwitchNavigator
for the authentication flow instead of a Stack at the top
as it replaces the routes so that you can never navigate back to the login/signup/splash once you get into the application and for accessing Tabs and Drawer inside stack/switch, you can wrap the Drawer inside your top level navigator and tab inside the drawer.
So you root navigation would look like this.
export default RootNavigation = createSwitchNavigator({
LoginScreen: {screen: LoginContainer},
Application: {screen: AppDrawer},
});
Your drawer navigator should be like the following:
const AppDrawer = createDrawerNavigator({
ApplicationTab: {screen: TabBar},
... other screen that you might want to use in drawer navigation.
}, {
contentComponent : (props) => <MyCustomDrawer {...props} />
});
and, Tab Navigator would be,
const TabBar = createBottomTabNavigator({
TabScreen1: {screen: Tab1},
... other tabs...
}, {
tabBarComponent : (props) => <MyTabBar {...props} />
});
If you put each of those navigators in single file then please do declare Tab before Drawer and Drawer before the Switch, else it would give errors.
In my experience, customising drawer navigator is very simple and fruitful but customising tab is not, there aren't proper API doc for the same and community answers are also somewhat misleading.
BUT, with normal use cases and for most of the vivid ones too, you can do your job without needing to override the default one as it is already highly operable and customisable in terms of icons, materialism and each tab exposes its on onPress
that can also be easily overriden.
and as you as the drawer is not getting operated from/via the header, then can you please ensure that the navigation
prop you are using to operate the drawer open
close
or toggle
action is the one given by drawer ?
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have usedthis.props.navigation.openDrawer()
from the CustomHeader, you should useprops.navigation.openDrawer()
and also ensure that you are passing the props to yourCustomHeader
correctly.
– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
|
show 4 more comments
You might wanna consider the SwitchNavigator
for the authentication flow instead of a Stack at the top
as it replaces the routes so that you can never navigate back to the login/signup/splash once you get into the application and for accessing Tabs and Drawer inside stack/switch, you can wrap the Drawer inside your top level navigator and tab inside the drawer.
So you root navigation would look like this.
export default RootNavigation = createSwitchNavigator({
LoginScreen: {screen: LoginContainer},
Application: {screen: AppDrawer},
});
Your drawer navigator should be like the following:
const AppDrawer = createDrawerNavigator({
ApplicationTab: {screen: TabBar},
... other screen that you might want to use in drawer navigation.
}, {
contentComponent : (props) => <MyCustomDrawer {...props} />
});
and, Tab Navigator would be,
const TabBar = createBottomTabNavigator({
TabScreen1: {screen: Tab1},
... other tabs...
}, {
tabBarComponent : (props) => <MyTabBar {...props} />
});
If you put each of those navigators in single file then please do declare Tab before Drawer and Drawer before the Switch, else it would give errors.
In my experience, customising drawer navigator is very simple and fruitful but customising tab is not, there aren't proper API doc for the same and community answers are also somewhat misleading.
BUT, with normal use cases and for most of the vivid ones too, you can do your job without needing to override the default one as it is already highly operable and customisable in terms of icons, materialism and each tab exposes its on onPress
that can also be easily overriden.
and as you as the drawer is not getting operated from/via the header, then can you please ensure that the navigation
prop you are using to operate the drawer open
close
or toggle
action is the one given by drawer ?
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have usedthis.props.navigation.openDrawer()
from the CustomHeader, you should useprops.navigation.openDrawer()
and also ensure that you are passing the props to yourCustomHeader
correctly.
– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
|
show 4 more comments
You might wanna consider the SwitchNavigator
for the authentication flow instead of a Stack at the top
as it replaces the routes so that you can never navigate back to the login/signup/splash once you get into the application and for accessing Tabs and Drawer inside stack/switch, you can wrap the Drawer inside your top level navigator and tab inside the drawer.
So you root navigation would look like this.
export default RootNavigation = createSwitchNavigator({
LoginScreen: {screen: LoginContainer},
Application: {screen: AppDrawer},
});
Your drawer navigator should be like the following:
const AppDrawer = createDrawerNavigator({
ApplicationTab: {screen: TabBar},
... other screen that you might want to use in drawer navigation.
}, {
contentComponent : (props) => <MyCustomDrawer {...props} />
});
and, Tab Navigator would be,
const TabBar = createBottomTabNavigator({
TabScreen1: {screen: Tab1},
... other tabs...
}, {
tabBarComponent : (props) => <MyTabBar {...props} />
});
If you put each of those navigators in single file then please do declare Tab before Drawer and Drawer before the Switch, else it would give errors.
In my experience, customising drawer navigator is very simple and fruitful but customising tab is not, there aren't proper API doc for the same and community answers are also somewhat misleading.
BUT, with normal use cases and for most of the vivid ones too, you can do your job without needing to override the default one as it is already highly operable and customisable in terms of icons, materialism and each tab exposes its on onPress
that can also be easily overriden.
and as you as the drawer is not getting operated from/via the header, then can you please ensure that the navigation
prop you are using to operate the drawer open
close
or toggle
action is the one given by drawer ?
You might wanna consider the SwitchNavigator
for the authentication flow instead of a Stack at the top
as it replaces the routes so that you can never navigate back to the login/signup/splash once you get into the application and for accessing Tabs and Drawer inside stack/switch, you can wrap the Drawer inside your top level navigator and tab inside the drawer.
So you root navigation would look like this.
export default RootNavigation = createSwitchNavigator({
LoginScreen: {screen: LoginContainer},
Application: {screen: AppDrawer},
});
Your drawer navigator should be like the following:
const AppDrawer = createDrawerNavigator({
ApplicationTab: {screen: TabBar},
... other screen that you might want to use in drawer navigation.
}, {
contentComponent : (props) => <MyCustomDrawer {...props} />
});
and, Tab Navigator would be,
const TabBar = createBottomTabNavigator({
TabScreen1: {screen: Tab1},
... other tabs...
}, {
tabBarComponent : (props) => <MyTabBar {...props} />
});
If you put each of those navigators in single file then please do declare Tab before Drawer and Drawer before the Switch, else it would give errors.
In my experience, customising drawer navigator is very simple and fruitful but customising tab is not, there aren't proper API doc for the same and community answers are also somewhat misleading.
BUT, with normal use cases and for most of the vivid ones too, you can do your job without needing to override the default one as it is already highly operable and customisable in terms of icons, materialism and each tab exposes its on onPress
that can also be easily overriden.
and as you as the drawer is not getting operated from/via the header, then can you please ensure that the navigation
prop you are using to operate the drawer open
close
or toggle
action is the one given by drawer ?
edited Nov 27 '18 at 8:54
answered Nov 26 '18 at 2:47
Suraj MalviyaSuraj Malviya
895415
895415
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have usedthis.props.navigation.openDrawer()
from the CustomHeader, you should useprops.navigation.openDrawer()
and also ensure that you are passing the props to yourCustomHeader
correctly.
– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
|
show 4 more comments
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have usedthis.props.navigation.openDrawer()
from the CustomHeader, you should useprops.navigation.openDrawer()
and also ensure that you are passing the props to yourCustomHeader
correctly.
– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
I have created a gist for you from one of my project in which i have developed custom drawer component. Hope this helps.
– Suraj Malviya
Nov 27 '18 at 3:21
Also I can see you have used
this.props.navigation.openDrawer()
from the CustomHeader, you should use props.navigation.openDrawer()
and also ensure that you are passing the props to your CustomHeader
correctly.– Suraj Malviya
Nov 27 '18 at 3:27
Also I can see you have used
this.props.navigation.openDrawer()
from the CustomHeader, you should use props.navigation.openDrawer()
and also ensure that you are passing the props to your CustomHeader
correctly.– Suraj Malviya
Nov 27 '18 at 3:27
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
props.navigation.OpenDrawer() also doesn't work
– Hannibal
Nov 27 '18 at 8:12
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
@Hannibal can you post your code of where you are using your CustomerHeader component ? Also you might want to check the gist I shared in above comment. It might be helpful for you for coding style.
– Suraj Malviya
Nov 27 '18 at 9:02
1
1
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
@ArchNoob I have gone to the doc and have seen that the options are still there. for eg. see this. Although I will try it myself with v3 surely. :)
– Suraj Malviya
Nov 29 '18 at 12:42
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472505%2freact-native-how-to-implement-drawer-and-tabbar-in-stack-navigator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown