Header is empty after react-navigation updated to v2 - react-native

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'
}
);

Related

How to add a drawer navigator React-Navigation v4

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();

Swipe between screens isn't working in react-native, react navigation

I am trying to get my app to swipe between screens using react navigation. I have tried setting swipeEnabled, animationEnabled and gesturesEnabled to true but nothing has worked so far.
I am new to react navigation and thought i would give it a try.
I am using createStackNavigator so i dont know if i need to change that.
import React, { Component } from 'react';
import { createBottomTabNavigator,
createStackNavigator, createAppContainer } from 'react-navigation';
const Worcester = createStackNavigator(
{
Wrh,
},
{
initalRouteName: Wrh,
defaultNavigationOptions: {
title: 'Worcester',
headerLeft: <ActionBarImage />,
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#000000'
},
tabBarOptions: {
labelStyle: {
fontSize: 40
}
},
headerStyle: {
backgroundColor: '#FFFAFA',
borderBottomColor: '#ffffff',
borderBottomWidth: 3,
},
HeaderTitle: 'Test',
backgroundColor: '#FFDEAD',
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
}
});
const Alex = createStackNavigator(
{
Alx,
},
{
initalRouteName: Alx,
defaultNavigationOptions: {
title: 'Redditch',
headerLeft: <ActionBarImage />,
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#000000'
},
tabBarOptions: {
labelStyle: {
fontSize: 20
}
},
headerStyle: {
backgroundColor: '#FFFAFA',
borderBottomColor: '#ffffff',
borderBottomWidth: 3,
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
},
},
});
const TabNavigator = createBottomTabNavigator(
{
Worcester: { screen: Worcester },
Redditch: { screen: Alex },
},
{
tabBarOptions: {
activeTintColor: 'blue',
inactiveTintColor: '#605F60',
inactiveBackgroundColor: 'grey',
activeBackgroundColor: '#FFFAFA',
labelStyle: {
fontSize: 20,
marginTop: 0,
paddingTop: 0
},
style: {
paddingTop: 10
},
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
},
}
);
export default createAppContainer(TabNavigator);
You should use a topTabNavigator/bottomTabNavigator to swipe between screens on the same stack.
Then you use your stacknavigators,screens like that :
...
import { createMaterialTopTabNavigator, createStackNavigator } from 'react-navigation';
...
const someTabNavigator= createMaterialTopTabNavigator(
{
SomeScreen: {
screen:TheScreen,
navigationOptions: {
tabBarAccessibilityLabel: 'The Screen',
tabBarLabel: ({ tintColor }) => <LabelTitle tintColor={tintColor} label="The Screen" />,
},
},
SomeOtherScreen: {
screen: TheOtherScreen,
navigationOptions: {
tabBarAccessibilityLabel: 'The Other Screen',
tabBarLabel: ({ tintColor }) => <LabelTitle tintColor={tintColor} label="The Other Screen" />,
},
},
},
{
// Configs and etc.
swipeEnabled: true,
lazy: true,
optimizationsEnabled: true,
animationEnabled: true,
tabBarPosition: 'top',
tabBarOptions: {
scrollEnabled: true,
style: {
backgroundColor: colors.white,
},
inactiveTintColor: inactiveTintLabelColor,
activeTintColor: activeTintLabelColor,
indicatorStyle: {
backgroundColor: colors.primaryColor,
},
tabStyle: {
width: screen.screenWidth >= 600 ? 210 : 120,
}
},
}
)

How can i import the defaultNavigationOptions property on a screen?

I'm trying to import the react-navigation property into my screens the problem is that I always import the same defaultNavigationOptions for the different Stacks then to optimize the code I want to create as a kind of function so I only import it once on each screen without having to write it many times as I did, then my code so they understand more.
const BloquesStack = createStackNavigator(
{
BLOQUES: {
screen: ScreenBloquesRedux,
navigationOptions: ({ navigation }) => {
return {
headerLeft: <ButtonMenu navigation={navigation} />,
headerRight: <ButtonHome navigation={navigation} />
};
}
},
DetalleBloques: {
screen: DetalleBloque
},
IntegrantesBloque: {
screen: IntegrantesBloque
}
},
{
defaultNavigationOptions: {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
}
);
export { BloquesStack };
const ComisionesStack = createStackNavigator(
{
COMISIONES: {
screen: ComisionesRedux,
navigationOptions: ({ navigation }) => {
return {
headerLeft: <ButtonMenu navigation={navigation} />,
headerRight: <ButtonHome navigation={navigation} />
};
}
}
},
{
defaultNavigationOptions: {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
}
export { ComisionesStack };
const DrawerNavigator = createDrawerNavigator(
{
Diputados: { screen: DiputadosStack },
Bloques: { screen: BloquesStack },
Interbloques: { screen: InterBloquesStack },
Comisiones: { screen: ComisionesStack },
Autoridades: { screen: AutoridadesStack },
"Sesion En Vivo": { screen: SesionEnVivoStack },
"Diputados TV": { screen: DiputadosTVStack },
"Reglamentos HCDN": { screen: PDFReglamentosStack }
},
{
contentComponent: CustomDrawerContentComponent,
drawerWidth: width / 2,
contentOptions: {
activeTintColor: white,
activeBackgroundColor: Gris_Drawer,
inactiveTintColor: "rgb(105,105,104)",
itemsContainerStyle: {
textAlign: "center"
},
labelStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 17,
marginTop: 8,
marginLeft: 10
}
},
iconContainerStyle: {
opacity: 1
}
}
);
I just want to import default Navigation Options I do not intend to modify my navigation, I just want to know if it can be done. Thank you very much already
Create a separate object with the defaultNavigationOptions
const defaultNavigationOptions = {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
const BloquesStack = createStackNavigator(
{ /* routes */ },
{ defaultNavigationOptions }
)
const ComisionesStack = createStackNavigator(
{ /* routes */ },
{ defaultNavigationOptions }
)

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!

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);