Passing this.state into static function - react-native

I am trying to set the headerTitle in the header bar to be the username of the logged in user: this.state.username. However, when I try to set navigationOptions to a function, I am unable to pass in the current state.
What would be the best way to set the headerTitle inside the static navigationOptions? I believe that navigationOptions must remain static, otherwise nothing will be displayed.
class profile extends React.Component {
static navigationOptions =
{
headerTitle: this.state.username
};
const TabStack = createBottomTabNavigator(
{
Home: {
screen: home,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={25} style={{ color: tintColor }} />
),
},
},
Explore: {
screen: explore,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="search" size={25} style={{ color: tintColor }} />
),
}
},
Profile: {
screen: profile,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="person" size={25} style={{ color: tintColor }} />
),
}
},
Create: {
screen: createCompetition,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="add" size={25} style={{ color: tintColor }} />
),
}
},
Payment: {
screen: payment,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="attach-money" size={25} style={{ color: tintColor }} />
),
title: 'Payment',
}
}
},
{
tabBarOptions: {
showIcon: true,
showLabel: false,
activeTintColor: 'black',
style: { backgroundColor: 'white', }
},
},
)
TabStack.navigationOptions = ({ navigation, screenProps }) => {
const childOptions = getActiveChildNavigationOptions(navigation, screenProps)
return {
title: childOptions.title,
headerLeft: childOptions.headerLeft,
headerRight: childOptions.headerRight,
}
}

So instead of using react navigation Header ive disabled it and used my own cutsom,
This is my Header.js :
import React, {Fragment, Component} from 'react';
import {View, Text, TouchableOpacity, Image, SafeAreaView} from 'react-native';
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from 'react-native-responsive-screen';
import {headerStyles} from '../style/headerStyles';
export default class Header extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View
style={{
flexDirection: 'row',
paddingBottom: hp('3.125%'),
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View style={{opacity: 0}}>
<Image source={require('../assets/images/crossIcon.png')} />
</View>
<View style={{flexShrink: 1}}>
<Text style={headerStyles.headerText}>{this.props.title}</Text>
</View>
<View style={{left: -20, paddingLeft: 10, marginLeft: 10}}>
<TouchableOpacity
style={headerStyles.imageView}
onPress={() => this.props.navigation.goBack()}>
<Image
style={{height: 15, width: 15}}
source={require('../assets/images/crossIcon.png')}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
and you can import in any component like :
import Header from '../components/Header';
return(
<Header title={this.state.userName} navigation={this.props.navigation} />
)
With this you have enough power to do anything you want with header.
Hope it helps. feel free for doubts.

I assume you are using react-navigation, I suggest you set a navigation param in your component and then get the param in the navigationOptions.
after login do this:
navigation.setParams({ headerTitle: this.state.userName})
and then inside your navigationOptions where you already have navigation:
return { title: navigation.getParam('headerTitle') }

Related

stacknavigator react native set title of top menu

i have a menu created like that :
import {createStackNavigator} from 'react-navigation';
const stackNav = createStackNavigator({
Main: {
screen: JourneeService,
navigationOptions: ({navigation}) => ({
headerTitle: <StackNavTitle title="Journée de Service " />,
headerStyle: {
backgroundColor: '#0088CE',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.openDrawer()}
style={{width: '150%'}}>
<Icon name="ios-menu" size={35} color="#fff" style={{padding: 10}} />
</TouchableOpacity>
),
}),
}
with StackNavTitle :
class StackNavTitle extends Component {
constructor(props){
super(props);
}
render() {
return <Text style={{fontWeight: 'bold', color:'#fff', fontSize:20}}>
{this.props.title}.
</Text>;
}
}
on code i retrieve a title by callback i want to set title dynamically how can i do that ?
thanks in advance for your responses
You can provide a function to the header which accepts three parameters as per the docs and one of them contains the dynamic title. Here is an example I took from the docs and update with your header component
import {createStackNavigator} from 'react-navigation';
const stackNav = createStackNavigator({
Main: {
screen: JourneeService,
navigationOptions: ({navigation}) => ({
headerTitle: ({allowFontScaling, style, children})=> <StackNavTitle title={children.title} />,
headerStyle: {
backgroundColor: '#0088CE',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
headerLeft: (
<TouchableOpacity
onPress={() => navigation.openDrawer()}
style={{width: '150%'}}>
<Icon name="ios-menu" size={35} color="#fff" style={{padding: 10}} />
</TouchableOpacity>
),
}),
}
You can find more info here https://reactnavigation.org/docs/4.x/stack-navigator#headertitle

React Native -- Even after imports and exports are correct getting error "The component for route 'Menu' must be a react component"

I have encountered the mentioned error. I searched for this through different links as. I changed import and export according to the answer to this question but the same error is still there. I have done imports and exports correctly.*
This is
MainComponent.js file where the Menu component is imported.
import React, { Component } from 'react';
import Menu from './MenuComponent';
import Dishdetail from './DishDetailComponent';
import Contact from './ContactComponent';
import About from './AboutComponent';
import Home from './HomeComponent';
import { View, Platform, Image, StyleSheet, ScrollView, Text } from 'react-native';
import { createStackNavigator, createDrawerNavigator, DrawerItems, SafeAreaView, SrollView } from 'react-navigation';
import { Icon } from 'react-native-elements';
import { connect } from 'react-redux';
import { fetchDishes, fetchComments, fetchPromos, fetchLeaders } from '../redux/ActionCreators';
const mapStateToProps = (state) => {
return {
dishes: state.dishes,
promotions: state.promotions,
leaders: state.leaders,
};
};
const mapDispatchToProps = (dispatch) => ({
fetchDishes: () => dispatch(fetchDishes()),
fetchComments: () => dispatch(fetchComments()),
fetchPromos: () => dispatch(fetchPromos()),
fetchLeaders: () => dispatch(fetchLeaders()),
});
const MenuNavigator = createStackNavigator(
{
Menu: {
screen: Menu,
navigationOptions: ({ navigation }) => ({
headerLeft: <Icon name="menu" size={24} color="#fff" onPress={() => navigation.toggleDrawer()} />,
}),
},
Dishdetail: { screen: Dishdetail },
},
{
initialRouteName: 'Menu',
navigationOptions: {
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff',
},
},
}
);
const HomeNavigator = createStackNavigator(
{
Home: { screen: Home },
},
{
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff',
},
headerLeft: <Icon name="menu" size={24} color="#fff" onPress={() => navigation.toggleDrawer()} />,
}),
}
);
const ContactNavigator = createStackNavigator(
{
Contact: { screen: Contact },
},
{
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff',
},
headerLeft: <Icon name="menu" size={24} color="#fff" onPress={() => navigation.toggleDrawer()} />,
}),
}
);
const AboutNavigator = createStackNavigator(
{
About: { screen: About },
},
{
navigationOptions: ({ navigation }) => ({
headerStyle: {
backgroundColor: '#512DA8',
},
headerTintColor: '#fff',
headerTitleStyle: {
color: '#fff',
},
headerLeft: <Icon name="menu" size={24} color="#fff" onPress={() => navigation.toggleDrawer()} />,
}),
}
);
const CustomDrawerContentComponent = (props) => (
<ScrollView>
<SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}>
<View style={styles.drawerHeader}>
<View style={{ flex: 1 }}>
<Image source={require('./images/logo.png')} style={styles.drawerImage} />
</View>
<View style={{ flex: 2 }}>
<Text style={styles.drawerHeaderText}>VangooApp</Text>
</View>
</View>
<DrawerItems {...props} />
</SafeAreaView>
</ScrollView>
);
const MainNavigator = createDrawerNavigator(
{
Home: {
screen: HomeNavigator,
navigationOptions: {
title: 'Home',
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => <Icon name="home" type="font-awesome" size={24} color={tintColor} />,
},
},
About: {
screen: AboutNavigator,
navigationOptions: {
title: 'About',
drawerLabel: 'About Us',
drawerIcon: ({ tintColor }) => (
<Icon name="info-circle" type="font-awesome" size={24} color={tintColor} />
),
},
},
Menu: {
screen: MenuNavigator,
navigationOptions: {
title: 'Menu',
drawerLabel: 'Menu',
drawerIcon: ({ tintColor }) => <Icon name="list" type="font-awesome" size={24} color={tintColor} />,
},
},
Contact: {
screen: ContactNavigator,
navigationOptions: {
title: 'Contact',
drawerLabel: 'Contact Us',
drawerIcon: ({ tintColor }) => (
<Icon name="address-card" type="font-awesome" size={22} color={tintColor} />
),
},
},
},
{
drawerBackgroundColor: '#D1C4E9',
contentComponent: CustomDrawerContentComponent,
}
);
class MainComponent extends Component {
componentDidMount() {
this.props.fetchDishes();
this.props.fetchComments();
this.props.fetchPromos();
this.props.fetchLeaders();
}
render() {
return (
<View style={{ flex: 1, paddingTop: Platform.OS === 'ios' ? 0 : Expo.Constants.statusBarHeight }}>
<MainNavigator />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
drawerHeader: {
backgroundColor: '#512DA8',
height: 140,
alignItems: 'center',
justifyContent: 'center',
flex: 1,
flexDirection: 'row',
},
drawerHeaderText: {
color: '#fff',
fontSize: 24,
fontWeight: 'bold',
},
drawerImage: {
margin: 10,
width: 80,
height: 60,
},
});
export default connect(mapStateToProps, mapDispatchToProps)(MainComponent);
MenuComponent.js
import React, { Component } from 'react';
import { FlatList } from 'react-native';
import { Tile } from 'react-native-elements';
import { connect } from 'react-redux';
import { baseUrl } from '../shared/baseURL';
const mapStateToProps = (state) => {
return {
dishes: state.dishes,
};
};
class Menu extends Component {
static navigationOptions = {
title: 'Menu',
};
render() {
const renderMenuItem = ({ item, index }) => {
return (
<Tile
key={index}
title={item.name}
caption={item.description}
featured
onPress={() => navigate('Dishdetail', { dishId: item.id })}
imageSrc={{ uri: baseUrl + item.image }}
/>
);
};
const { navigate } = this.props.navigation;
return (
<FlatList
data={this.props.dishes.dishes}
renderItem={renderMenuItem}
keyExtractor={(item) => item.id.toString()}
/>
);
}
}
export default connect(mapStateToProps)(Menu);
[![Error][1]][1]

React-navigation: how to style a big middle button in tab navigation?

So I have this requirement. To make tab navigation with this exact appearance:
I had no problem styling the tab bar with the gradient and the buttons. I created my own custom one with this code:
export default createBottomTabNavigator({
(... routes here)
}, {
initialRouteName: "Inspiration",
tabBarComponent: props => <BottomTabBar {...props} />
})
But now I'm having trouble with the middle button. My bar looks like this:
If I remove the custom tab bar removing this line:
tabBarComponent: props => <BottomTabBar {...props} />
Then now my middle button looks how it should, but of course, all of the other styles are missing:
This is how my BottomTabBar component looks right now:
import React from "react";
import { Image, StyleSheet, Text, TouchableOpacity } from "react-native";
import { TabBarBottomProps, NavigationRoute } from "react-navigation";
import LinearGradient from "react-native-linear-gradient";
const iconBag = require("./images/bag.png");
export default function BottomTabBar(props: TabBarBottomProps) {
const {
renderIcon,
getLabelText,
activeTintColor,
inactiveTintColor,
onTabPress,
onTabLongPress,
getAccessibilityLabel,
navigation
} = props;
const { routes, index: activeRouteIndex } = navigation.state;
function renderTabBarButton(routeIndex, route) {
const isRouteActive = routeIndex === activeRouteIndex;
const tintColor = isRouteActive ? activeTintColor : inactiveTintColor;
if (route.key == "Bag")
return <Image style={{ width: 100, height: 100 }} source={iconBag} />;
return (
<TouchableOpacity
key={routeIndex}
style={styles.tabButton}
onPress={() => onTabPress({ route })}
onLongPress={() => onTabLongPress({ route })}
accessibilityLabel={getAccessibilityLabel({ route })}
>
{renderIcon({ route, focused: isRouteActive, tintColor })}
<Text style={styles.tabText}>{getLabelText({ route })}</Text>
</TouchableOpacity>
);
}
return (
<LinearGradient colors={["#FFFFFF", "#DEDEDE"]} style={styles.container}>
{routes.map((route, routeIndex) => renderTabBarButton(routeIndex, route))}
</LinearGradient>
);
}
const styles = StyleSheet.create({
container: {
height: 60,
flexDirection: "row",
alignItems: "center",
borderWidth: 1,
borderColor: "#C4C4C4"
},
tabButton: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
tabText: {
fontFamily: "Source Sans Pro",
fontSize: 11,
color: "#454545",
marginTop: 1
}
});
What can I do? Any help will be really much appreciated!
You can try my solution
const TabNavigator = createBottomTabNavigator(
{
Top: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({tintColor}) => (
<Image
source={require('./src/assets/ic_list.png')}
style={{width: 24, height: 24, tintColor: tintColor}}
/>
),
},
},
New: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({tintColor}) => (
<Image
source={require('./src/assets/ic_clock.png')}
style={{width: 24, height: 24, tintColor: tintColor}}
/>
),
},
},
Ask: { // big plus icon in the middle
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({tintColor}) => (
<View
style={{
position: 'absolute',
bottom: 20, // space from bottombar
height: 58,
width: 58,
borderRadius: 58,
backgroundColor: '#5a95ff',
justifyContent: 'center',
alignItems: 'center',
}}>
<Image
source={require('./src/assets/ic_add_24.png')}
style={{
width: 40,
height: 40,
tintColor: '#f1f6f9',
alignContent: 'center',
}}
/>
</View>
),
},
},
Show: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({tintColor}) => (
<Image
source={require('./src/assets/ic_notification.png')}
style={{width: 24, height: 24, tintColor: tintColor}}
/>
),
},
},
Jobs: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({tintColor}) => (
<Image
source={require('./src/assets/ic_person.png')}
style={{width: 24, height: 24, tintColor: tintColor}}
/>
),
},
},
},
{
tabBarOptions: {
activeTintColor: '#FF6F00',
inactiveTintColor: '#8997B0',
showIcon: true,
showLabel: false,
style: {
backgroundColor: '#f1f6f9',
},
},
},
);
I made this bottom tab with react native. I think your design is very simple. My code sample will help you I think.
import React from 'react';
import { createBottomTabNavigator, createStackNavigator } from 'react-navigation';
import { View, Image } from 'react-native'
import { Text } from 'native-base';
import Featured from './featured';
import AboutUs from './about_us';
import Shop from './shop';
import Booking from './booking';
import Settings from './settings';
import styles from './styles';
import star from './../../assets/images/icons/star.png';
import star_check from './../../assets/images/icons/star_check.png';
import about from './../../assets/images/icons/about.png';
import about_check from './../../assets/images/icons/about_check.png';
import book from './../../assets/images/icons/book.png';
import check from './../../assets/images/icons/check.png';
import shop from './../../assets/images/icons/shop.png';
import shop_check from './../../assets/images/icons/shop_check.png';
import more from './../../assets/images/icons/more.png';
import more_check from './../../assets/images/icons/more_check.png';
const Tabs = createBottomTabNavigator(
{
Featured: {
screen: Featured,
navigationOptions: {
title: 'Featured',
tabBarIcon: ({ tintColor, focused }) => (
<View style={styles.tab}>
<Image source={focused? star_check : star} style={styles.icon} />
<Text style={[styles.name, {color: tintColor}]}>Kampanjer</Text>
</View>
)
}
},
AboutUs: {
screen: AboutUs,
navigationOptions: {
title: 'About Us',
tabBarIcon: ({ tintColor, focused }) => (
<View style={styles.tab}>
<Image source={focused? about_check : about} style={styles.icon} />
<Text style={[styles.name, {color: tintColor}]}>Om oss</Text>
</View>
)
}
},
Booking: {
screen: Booking,
navigationOptions: {
title: 'MIN SALONG',
tabBarIcon: ({ tintColor, focused }) => (
<View style={styles.book}>
<Image source={focused? check : book} style={styles.book_icon} />
</View>
)
}
},
Shop: {
screen: Shop,
navigationOptions: {
title: 'Shop',
tabBarIcon: ({ tintColor, focused }) => (
<View style={styles.tab}>
<Image source={focused? shop_check : shop} style={styles.icon} />
<Text style={[styles.name, {color: tintColor}]}>Shop</Text>
</View>
)
}
},
Settings: {
screen: Settings,
navigationOptions: {
title: 'More',
tabBarIcon: ({ tintColor, focused }) => (
<View style={styles.tab}>
<Image source={focused? more_check : more} style={styles.icon} />
<Text style={[styles.name, {color: tintColor}]}>Inställningar</Text>
</View>
)
}
},
},
{
initialRouteName: 'Featured',
tabBarOptions: {
activeTintColor: '#80A0AB',
inactiveTintColor: '#fff',
showLabel: false,
style: {
height: 60,
backgroundColor: '#485155'
},
labelStyle: {
fontSize: 12,
fontFamily: 'Abel-Regular'
}
}
}
);
export default createStackNavigator({Tabs}, {headerMode: "none"});
Try this
<Tab.Navigator
tabBarOptions={{
activeTintColor: 'red',
// activeBackgroundColor:'#000',
//inactiveBackgroundColor:'red',
labelStyle: {
position: 'absolute',
top: constants.vh(35),
fontSize:constants.vh(18),
}
}}
>
<Tab.Screen name='Home' //bottom tab for Home
options={{
tabBarIcon: ({ focused }) => {
let iconName;
iconName = focused ? constants.images.bottomHome : constants.images.bottomHome //for icon or image
return (
<View>
<Image source={iconName} style={{ width: constants.vw(40), height: constants.vh(25) ,position:'absolute',right:constants.vw(-20),bottom:constants.vh(-5)}} resizeMode="contain" />
</View>
)
}
}}
component={HomeScreen} />
</Tab.Navigator>

React Navigation + react-navigation-material-bottom-tabs

I am trying to create an app that has a bottom navigation tab with the four screen tab menu. I want to have another page for Admin but the menu option should not appear on the bottom tab. ( I have a to go to that page) I am using react-navigation-material-bottom-tabs to create the bottom tab bar.
I need a way to go to that page.
export default createMaterialBottomTabNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: {
title: "Home",
tabBarLabel: <Text style={{ color: "white" }}>Home</Text>,
barStyle: { backgroundColor: "#281b39" },
tabBarIcon: <Icon size={24} name="home" style={{ color: "white" }} />
}
},
Announcement: {
screen: AnnouncementScreen,
navigationOptions: {
title: "Announcement",
tabBarLabel: <Text style={{ color: "white" }}>Announcements</Text>,
barStyle: { backgroundColor: "#0e141d" },
tabBarIcon: (
<Icon size={24} name="bullhorn" style={{ color: "white" }} />
)
}
},
Material: {
screen: MaterialScreen,
navigationOptions: {
title: "Materials",
tabBarLabel: <Text style={{ color: "white" }}>Materials</Text>,
barStyle: { backgroundColor: "#E64A19" },
tabBarIcon: (
<Icon size={24} name="calendar" style={{ color: "white" }} />
)
}
},
Contact: {
screen: ContactScreen,
navigationOptions: {
title: "Contact",
tabBarLabel: <Text style={{ color: "white" }}>Contact</Text>,
barStyle: { backgroundColor: "#524365" },
tabBarIcon: (
<Icon size={24} name="comments" style={{ color: "white" }} />
)
}
}, },
{
shifting: true,
labeled: true } );
Here is an updated version
import React, { Component } from "react";
import { AppRegistry, Text, View, StatusBar } from "react-native";
import Icon from "react-native-vector-icons/FontAwesome";
import { createStackNavigator, createAppContainer } from "react-navigation";
import { createMaterialBottomTabNavigator } from "react-navigation-material-bottom-tabs";
import Home from "./app/components/home.js";
import Announcements from "./app/components/announcements.js";
import Contact from "./app/components/contact.js";
import BackgroundImage from "./app/components/BackgroundImage.js";
class HomeScreen extends Component {
render() {
return (
<BackgroundImage>
<StatusBar hidden={true} />
<Home />
</BackgroundImage>
);
}
}
class AnnouncementScreen extends Component {
render() {
return (
<Announcements>
<StatusBar hidden={true} />
</Announcements>
);
}
}
class MaterialScreen extends Component {
render() {
return (
<View>
<Text style={{ textAlign: "center", top: 200 }}>
This is going to be the Materials Screen
</Text>
</View>
);
}
}
class ContactScreen extends Component {
render() {
return <Contact />;
}
}
class AdminPage extends Component {
render() {
return <Text>Hi</Text>;
}
}
const Admins = {
Admin: {
screen: AdminPage
}
};
const ContactStack = createStackNavigator({
Contact: {
screen: ContactScreen
},
...Admins
});
const AppNavigator = createMaterialBottomTabNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: {
title: "Home",
tabBarLabel: <Text style={{ color: "white" }}>Home</Text>,
barStyle: { backgroundColor: "#281b39" },
tabBarIcon: <Icon size={24} name="home" style={{ color: "white" }} />
}
},
Announcement: {
screen: AnnouncementScreen,
navigationOptions: {
title: "Announcement",
tabBarLabel: <Text style={{ color: "white" }}>Announcements</Text>,
barStyle: { backgroundColor: "#0e141d" },
tabBarIcon: (
<Icon size={24} name="bullhorn" style={{ color: "white" }} />
)
}
},
Material: {
screen: MaterialScreen,
navigationOptions: {
title: "Materials",
tabBarLabel: <Text style={{ color: "white" }}>Materials</Text>,
barStyle: { backgroundColor: "#E64A19" },
tabBarIcon: (
<Icon size={24} name="calendar" style={{ color: "white" }} />
)
}
},
Contact: {
screen: ContactStack,
navigationOptions: {
title: "Contact",
tabBarLabel: <Text style={{ color: "white" }}>Contact</Text>,
barStyle: { backgroundColor: "#524365" },
tabBarIcon: (
<Icon size={24} name="comments" style={{ color: "white" }} />
)
}
}
},
{
shifting: true,
labeled: true
}
);
const App = createAppContainer(AppNavigator);
export default App;
You can make use of a StackNavigator to go to a new screen.
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import { createMaterialBottomTabNavigator } from "react-navigation-material-bottom-tabs";
class Home extends React.Component {
render() {
const { navigation } = this.props;
return (
<View style={styles.container}>
<Text onPress={() => navigation.navigate("Admin")}>Home</Text>
</View>
);
}
}
class Announcement extends React.Component {
render() {
const { navigation } = this.props;
return (
<View style={styles.container}>
<Text onPress={() => navigation.navigate("Admin")}>Announcement</Text>
</View>
);
}
}
class Material extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Material</Text>
</View>
);
}
}
class Contact extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Contact</Text>
</View>
);
}
}
class Admin extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Admin</Text>
</View>
);
}
}
const commonScreens = {
Admin: {
screen: Admin
}
};
const HomeStack = createStackNavigator({
Home: {
screen: Home
},
...commonScreens
});
const AnnouncementStack = createStackNavigator({
Announcement: {
screen: Announcement
},
...commonScreens
});
const AppNavigator = createMaterialBottomTabNavigator(
{
Home: { screen: HomeStack },
Announcement: { screen: AnnouncementStack },
Material: { screen: Material },
Contact: { screen: Contact }
},
{
initialRouteName: "Home",
activeColor: "#f0edf6",
barStyle: { backgroundColor: "#694fad" },
labeled: true
}
);
export default createAppContainer(AppNavigator);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});
There are options for removing the header as well.
For more information please have a look at the docs https://reactnavigation.org/docs/en/stack-navigator.html#stacknavigatorconfig

showing navigation bottom bar in screens which are not part of bottom tabs in react native

I have three tabs and 8 screens in react native app. I have created bottom tab navigator as follows and I have 5 more screens which i dont want in tabs but on those screens the normal three tabs bottom bar navigator is required. Exact help is appreciated.
const TabNavigation = createBottomTabNavigator(
{
Scan: {
screen: ScanScreen,
navigationOptions: {
tabBarLabel: 'Scan',
tabBarIcon: () => <Ionicons name="ios-qr-scanner-outline" size={32} color="blue" />
,
},
},
Patient: {
screen: PatientStack,
navigationOptions: {
tabBarLabel: 'Patients',
tabBarIcon: () => <Ionicons name="ios-people" size={32} color="blue" />
,
},
},
Setting: {
screen: SettingScreen,
navigationOptions: {
tabBarLabel: 'Setting',
tabBarIcon: () => <Ionicons name="ios-settings" size={32} color="blue" />
,
},
},
},
{
lazyLoad: true,
animationEnabled: false,
tabBarPosition: 'bottom',
tabBarOptions: {
showIcon: true,
showLabel: true,
activeTintColor: '#7117ea',
inactiveTintColor: '#7117ea',
style: {
backgroundColor: '#eff0f1'
},
iconStyle: {
width: 40
},
tabStyle: {
height: 60
}
},
},
);
This is an Example code which will solve your problem (Hope so)
In this there are three scenes and in bottom bar two screens are rendered and the third scenes is rendered after clicking a link but the bottom bar will be there.
import React from 'react';
import { Button, Text, View } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
<Button
title="Go to Settings"
onPress={() => this.props.navigation.navigate('Settings')}
/>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}
/>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
<Button
title="Go to Home"
onPress={() => this.props.navigation.navigate('Home')}
/>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}
/>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Details!</Text>
</View>
);
}
}
const HomeStack = createStackNavigator({
Home: { screen: HomeScreen },
Details: { screen: DetailsScreen },
});
const SettingsStack = createStackNavigator({
Settings: { screen: SettingsScreen },
Details: { screen: DetailsScreen },
});
export default createBottomTabNavigator(
{
Home: { screen: HomeStack },
Settings: { screen: SettingsStack },
},
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === 'Home') {
iconName = `ios-information-circle${focused ? '' : '-outline'}`;
} else if (routeName === 'Settings') {
iconName = `ios-options${focused ? '' : '-outline'}`;
}
// You can return any component that you like here! We usually use an
// icon component from react-native-vector-icons
return <Ionicons name={iconName} size={25} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'red',
inactiveTintColor: 'gray',
},
}
);