How to add a drawer navigator React-Navigation v4 - react-native

I have this code for creating a drawer navigator. But I can't open my drawer through left to right slide. Can anyone please help me to solve my problem. I am new to react-native. I am using react-navigation v4 and react-navigation-drawer for the drawer navigator.
const EMBotomTabScreen = createDrawerNavigator ({
SUMMARY: {
screen : EMNavigator,
navigationOptions :{
animationEnabled : 'true',
// headerLeft : <HamburgerIcon navigationProps={ navigation }/>,
swipeEnabled: true,
tabBarOptions: {
activeTintColor: '#369841',
//activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
borderRightWidth:2,
borderRightColor:'blue',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
// borderRightWidth:2,
// borderRightColor:'blue'
},
indicatorStyle: {
backgroundColor: 'red',
},
},
tabStyle: {
backgroundColor: 'green',
borderRightWidth:2,
borderRightColor:'blue'
},
indicatorStyle : {
backgroundColor : 'red'
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `ios-desktop`;
return <Image source={require('../assets/aaaaa.png')} size={27} color={tintColor} />;
},
}
},
DEMAND: {
screen : EMDemandNavigator,
navigationOptions : {
tabBarOptions: {
activeTintColor: '#369841',
// activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
},
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `ios-cloud`;
return <Image source={require('../assets/aaaaaaa.png')} size={27} color={tintColor} />;
},
}
},
ALARM: {
screen : EMAlarmNavigator,
navigationOptions : {
animationEnabled : 'true',
swipeEnabled: true,
tabBarOptions: {
activeTintColor: '#369841',
//activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
borderRightWidth:2,
borderRightColor:'blue',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
// borderRightWidth:2,
// borderRightColor:'blue'
},
indicatorStyle: {
backgroundColor: 'red',
},
},
tabStyle: {
backgroundColor: 'green',
borderRightWidth:2,
borderRightColor:'blue'
},
indicatorStyle : {
backgroundColor : 'red'
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `bell-o`;
return <Icons name={iconName} size={27} color={tintColor} />;
},
}
}

You can get an idea from the below given code.
import Splash from '../Splash/Splash';
import Home from '../screens/Home';
import DrawerScreen from '../screens/DrawerScreen'
//And many more
import {createStackNavigator,
createSwitchNavigator,
createAppContainer,
createDrawerNavigator} from 'react-navigation';
const AppStack = createDrawerNavigator({
Home:Home,
},{
contentComponent: DrawerScreen,
defaultNavigationOptions: {
drawerWidth: Dimensions.get('window').width - 120,
},
});
const AppNavigator = createStackNavigator({
Splash:Splash,
Login:Login,
SignUp:SignUp },,{
defaultNavigationOptions: {
header: null,
gesturesEnabled: false
},
initialRouteName: "Splash");
const AppContainer = createAppContainer(AppNavigator);
Whenever you want to open the drawer like on click of the drawer icon. then add these line in the onPress definition.
this.props.navigation.openDrawer();

Related

Indicator is not rendering on iOS and Android

I am trying to render the topBar on tab navigation and it's not showing
import { createBottomTabNavigator, createAppContainer } from "react-navigation";
const TabNavigator = createBottomTabNavigator(
{
Dashboard: {
screen: Dashboard,
navigationOptions: {
title: "Dashboard",
tabBarIcon: ({ tintColor }) => (
<Icon
name="home"
type="Entypo"
style={{
color: tintColor,
}}
color={tintColor}
/>
),
},
},
},
{
tabBarPosition: "top",
tabBarOptions: {
activeTintColor: "#5D10F6",
inactiveTintColor: "gray",
showIcon: true,
tintColor: "#333",
animationEnabled: false,
showLabel: false,
scrollEnabled: true,
tabBarPosition: "top",
indicatorStyle: {
backgroundColor: "red",
},
},
}
);
Note: createTabNavigator is removed in react-navigation#3.x. Please use createBottomTabNavigator and/or createMaterialTopTabNavigator instead.
However createBottomTabNavigator no longer supports indicatorStyle :(

I couldn't use react-navigation tab bar. How to use this?

Now I am trying to create React Native app on Expo and use React-Navigation Tab Bar but I could't.
Actually I don't get any error but this code below doesn't work.
No warning as well.
import { createBottomTabNavigator, createAppContainer } from 'react-
navigation';
import Icon from 'react-native-vector-icons/FontAwesome';
import Home from './src/Screens/Home';
import Help from './src/Screens/Help';
const App = createBottomTabNavigator(
{
Home: {
screen: Home,
defaultNavigationOptions: {
tabBarIcon: ({ tintColor }) => {
<Icon name="home" style={{ width: 25, height: 25, tintColor:
tintColor}}/>
},
title: 'Home',
},
},
Help: { screen: Help },
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
},
},
);
export default createAppContainer(App);
the tab works fine, but if you meant, there's no icon, try this instead
navigationOptions: {
tabBarIcon: ({ tintColor, activeTintColor }) => (
<Icon name="home" size={24} color={tintColor} />)
},
Please try to implement this way. This is copy of my tabNavigator. Hope this will be helpful for you.
const TabRouter = createBottomTabNavigator(
{
HomeAfterLoginScreen: { screen: A },
ShowListAlertScreen: { screen: B },
ShowListProfessionScreen: { screen: C },
MyAccountScreen: { screen: F }
},
{
tabBarPosition: "bottom",
tabBarOptions: {
style: { backgroundColor: "#50bcb8" },
showIcon: true,
showLabel: true,
gesturesEnabled: true,
indicatorStyle: { borderBottomWidth: 3, borderBottomColor: Style.color },
inactiveTintColor: "#fff",
activeTintColor: "#fff",
tabStyle: { justifyContent: "center", alignItems: "center" }
}
});
I got it. I have solved this issue.
const App = createBottomTabNavigator(
{
Favorite: {
screen: FavoriteShops,
navigationOptions: {
tabBarLabel: 'お気に入り',
tabBarIcon: ({ tintColor }) => (
<Icon name="heart" size={25} color={tintColor} />
),
},
},
Home: {
screen: Home,
navigationOptions: {
tabBarLabel: 'ホーム',
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={30} color={tintColor} />
),
},
},
Help: {
screen: Help,
navigationOptions: {
tabBarLabel: 'その他',
tabBarIcon: ({ tintColor }) => (
<Icon name="bars" size={25} color={tintColor} />
),
},
},
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
showIcon: true,
showLabel: true,
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
style: {
width: '100%',
height: 70,
},
tabStyle: {
paddingTop: 20,
},
},
},
);

React navigation How can I change the header navigation title dynamically for each tab?

I have created 3 screens which display as tabs on the createMaterialTopTabNavigator which is inside a stack navigator my issue is i dont know how to dynamically set Header title for each tab. currently setting the title to "welcome" applies to all the tabs. Any help please?
xxxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxxx
import { LinearGradient } from 'expo';
import { Icon } from "native-base";
import React from 'react';
import { StyleSheet, View } from 'react-native';
import { createMaterialTopTabNavigator, DrawerActions } from 'react-navigation';
import Home from '../TabNavigator/Home';
import MyProfile from '../TabNavigator/MyProfile';
import SelectAirtime from '../TabNavigator/SelectAirtime';
export default class TabNavigator extends React.Component {
static navigationOptions = ({ navigation, }) => {
return {
title: "Welcome",
headerLeft: (
<View style={{ padding: 10, }}>
<Icon style={{ color: '#fff', fontSize: 24 }} name='menu' type="MaterialCommunityIcons"
onPress={() => navigation.dispatch(DrawerActions.openDrawer())} />
</View>
),
headerBackground: (
<LinearGradient
colors={['#2acc55', '#10356c']}
style={{ flex: 1 }}
start={[0, 0]}
end={[1, 0]}
/>
),
headerTitleStyle: { color: '#fff' },
}
}
render() {
return (
<HomeScreenTabNavigator></HomeScreenTabNavigator>
);
}
}
const HomeScreenTabNavigator = createMaterialTopTabNavigator({
Home: {
screen: Home, navigationOptions: {
tabBarIcon: ({ tintColor }) => (<Icon style={{ color: 'white', fontSize: 24 }} name='home' type="MaterialCommunityIcons" />),
}
},
"Buy AirTime": {
screen: SelectAirtime, navigationOptions: {
tabBarIcon: ({ tintColor }) => (<Icon style={{ color: 'white', fontSize: 24 }} name='cards-outline' type="MaterialCommunityIcons" />),
}
},
"Account": {
screen: MyProfile, navigationOptions: {
tabBarIcon: ({ tintColor }) => (<Icon style={{ color: 'white', fontSize: 24 }} name='user' type="EvilIcons" />),
}
},
},
{
initialRouteName: 'Home',
tabBarPosition: 'bottom',
tabBarOptions: {
activeTintColor: 'white',
inactiveTintColor: '#f2f2f2',
labelStyle: {
fontSize: 9,
},
tabStyle: {
height: 60,
},
style: {
backgroundColor: '#1e3c72',
borderBottomColor: '#1e3c72',
},
indicatorStyle: {
height: 0,
},
showIcon: true,
}
}
)
Define TabNavigator:
import { LinearGradient } from 'expo';
import { Icon } from "native-base";
import React from 'react';
import { StyleSheet, View } from 'react-native';
import { createMaterialTopTabNavigator, DrawerActions } from 'react-navigation';
import Home from '../TabNavigator/Home';
import MyProfile from '../TabNavigator/MyProfile';
import SelectAirtime from '../TabNavigator/SelectAirtime';
const HomeScreenTabNavigator = createMaterialTopTabNavigator({
Home: {
screen: Home,
navigationOptions: {
tabBarIcon: ({ tintColor, homeTitle }) => ( < Icon style = { { color: 'white', fontSize: 24 } } name = 'home'
type = "MaterialCommunityIcons" / > ),
tabBarLabel: homeTitle,
}
},
"Buy AirTime": {
screen: SelectAirtime,
navigationOptions: {
tabBarIcon: ({ tintColor, selectAirtimeTitle }) => ( < Icon style = { { color: 'white', fontSize: 24 } } name = 'cards-outline'
type = "MaterialCommunityIcons" / > ),
tabBarLabel: selectAirtimeTitle,
}
},
"Account": {
screen: MyProfile,
navigationOptions: {
tabBarIcon: ({ tintColor, myProfileTitle }) => ( < Icon style = { { color: 'white', fontSize: 24 } } name = 'user'
type = "EvilIcons" / > ),
tabBarLabel: myProfileTitle,
}
},
}, {
initialRouteName: 'Home',
tabBarPosition: 'bottom',
tabBarOptions: {
activeTintColor: 'white',
inactiveTintColor: '#f2f2f2',
labelStyle: {
fontSize: 9,
},
tabStyle: {
height: 60,
},
style: {
backgroundColor: '#1e3c72',
borderBottomColor: '#1e3c72',
},
indicatorStyle: {
height: 0,
},
showIcon: true,
}
})
export default HomeScreenTabNavigator;
use it:
<HomeScreenTabNavigator
screenProps={{
homeTitle: 'This home title',
selectAirtimeTitle: 'This airtime title',
myProfileTitle: 'This profile title',
}}
/>
I hope useful to you!

Header is empty after react-navigation updated to v2

After updating react-navigation package of my react-native project from 1.5.8 to 2.0.1 my header became empty on all tabs of TabNavigator. On other screens it works as before.
Example of tab Dashboard:
class Dashboard extends PureComponent {
static navigationOptions = ({ navigation }) => {
return {
title: strings.dashboard_header_title,
headerLeft: renderMenu(navigation)
};
};
...
}
export default connect((state, ownProps) => {
...
})(Dashboard);
const renderMenu = navigation => {
return <ImageButton
style={styles.padding}
imageStyle={styles.tintWhite}
image={require('../../../res/images/menu.png')}
onPress={navigation.state.params && navigation.state.params.toggleSideMenu}/>
}
It worked with old version of react-navigation. I use react-redux and tab navigator inside stack navigator.
TabNavigator:
const routeConfig = {
Dashboard: {
screen: Dashboard,
resource: 'dashboard',
navigationOptions: { tabBarIcon: ({tintColor}) => <Image style={{tintColor}} source={require('../../../res/images/tab-dashboard.png')}/> }
},
...
};
const drawConfig = {
lazy: false,
animationEnabled: false,
swipeEnabled: false,
tabBarPosition: 'bottom',
tabBarOptions: {
upperCaseLabel: false,
activeBackgroundColor: 'white',
activeTintColor: colors.main,
inactiveTintColor: colors.normal,
showIcon: true,
style: {
height: 48,
backgroundColor: 'white',
borderTopColor: colors.border,
borderTopWidth: values.borderWidth,
elevation: 0
},
labelStyle: {
fontSize: 11,
marginBottom: 0
},
tabStyle: {
padding: 3,
paddingTop: Platform.OS === 'android' ? 4 : 3
},
indicatorStyle: {
height: 0,
width: 0
}
}
}
const InnerTabNavigator = createBottomTabNavigator(routeConfig, drawConfig);
class TabBarNavigator extends PureComponent
{
...
render() {
return (
<InnerTabNavigator
{...this.props.ownProps}
navigation={{
...this.props.navigation,
state: this.state
}}
/>
);
}
...
}
TabBarNavigator.router = InnerTabNavigator.router;
MainNavigator:
const MainNavigatorInner = createStackNavigator({
...
TabBarNavigator: { screen: TabBarNavigator },
...
}, {
initialRouteName: 'SignIn',
headerMode: 'screen',
navigationOptions: ({ navigation }) => {
return {
headerTintColor: 'white',
headerTitleStyle: styles.headerTitle,
headerStyle: {
backgroundColor: colors.main,
shadowColor: 'transparent',
elevation: 0,
borderBottomWidth: values.borderWidth,
borderBottomColor: colors.borderHeader,
height: values.headerHeight
},
};
},
cardStyle: {
backgroundColor: colors.background
}
});
export default class MainNavigator extends PureComponent {
...
render() {
return (
...
<MainNavigatorInner
ref='navigation'
screenProps={this.screenProps}
onNavigationStateChange={this._onNavigationStateChange}/>
...
);
}
...
}
I had the same issue yesterday when I updated my react-navigation too. I know that this is not optimal but try adding the tabBarIcon in your drawconfig:
const drawConfig = {
lazy: false,
animationEnabled: false,
swipeEnabled: false,
tabBarPosition: 'bottom',
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let icon;
if (routeName === "SomeRouteName") {
icon = require("src/assets/someFile");
} else if (routeName === "Some other route name") {
if (focused) {
icon = require("src/assets/someFile.png");
} else {
icon = require("src/assets/someFile.png");
}
}
return <Image source={icon} style={{ width: 30, height: 30 }} />;
}
}),
tabBarOptions: {
upperCaseLabel: false,
activeBackgroundColor: 'white',
activeTintColor: colors.main,
inactiveTintColor: colors.normal,
showIcon: true,
style: {
height: 48,
backgroundColor: 'white',
borderTopColor: colors.border,
borderTopWidth: values.borderWidth,
elevation: 0
},
labelStyle: {
fontSize: 11,
marginBottom: 0
},
tabStyle: {
padding: 3,
paddingTop: Platform.OS === 'android' ? 4 : 3
},
indicatorStyle: {
height: 0,
width: 0
}
}
}
I had same ıssue too my solution is added my stacknavigator
headerMode: 'auto' property, I using expo template and RootNavigation file is added headerMode: 'auto' too, it worked for me,
Roote navigation like this =>
const RootStackNavigator = createStackNavigator(
{
Main: {
screen: MainTabNavigator,
},
},
{
navigationOptions: () => ({
headerTitleStyle: {
fontWeight: 'normal',
},
}),
headerMode: 'auto'
}
);

React native navigation custom tabbar icon

Is this possible to have custom component ( Button or what ever ) instead of simple icon in tab bar?
I need to set my tab bar icon dynamically like this
this.props.navigator.setTabButton({
tabIndex: 0,
icon: <Icon name="heart" size={28} /> <--- not appear
});
OR we can use only icons? Any solutions?
Yes, it is possible. I am sharing my solution as code below:
const renderNav = (routeName, focused) => {
return (
<View style={{
flex: 1,
width: 90,
justifyContent: 'center',
alignItems: 'center',
borderTopColor: StylesGeneric.COLORS.primary,
borderTopWidth: focused ? 4 : 0,
}}>
<Text
style={[StylesGeneric.STYLES.footerText]}>
{routeName}
</Text>
</View>
);
};
const customTabs = ({navigation}) => ({
tabBarIcon: ({focused}) => {
const {routeName} = navigation.state;
if (routeName === 'Home') {
return renderNav('Home', focused);
} else if (routeName === 'Profile') {
return renderNav('Profile', focused);
} else if (routeName === 'ProfileOther') {
return renderNav('ProfileOther', focused);
}
},
});
const nav = createBottomTabNavigator(
{
Home: {
screen: Home,
},
Profile: {
screen: Profile,
},
ProfileOther: {
screen: ProfileOther,
},
},
{
defaultNavigationOptions: customTabs,
animationEnabled: true,
swipeEnabled: true,
tabBarPosition: 'bottom',
initialRouteName: 'Home',
tabBarOptions: {
showLabel: false,
style: {
borderTopColor: 'transparent',
backgroundColor: StylesGeneric.COLORS.white,
height: StylesGeneric.FOOTER_HEIGHT,
},
},
},
);
const AppContainer = createAppContainer(nav);