I have created a basic react-native app which consists of a few buttons that are used to render different screens. After the user has logged in, I render a TabNavigator item but for some reason the no tabs appear and the screen is completely blank. I have comparing my code to others without any luck. Any suggestions?
import React from 'react';
import { StyleSheet, Text, View, Image, Button, ImageBackground } from 'react-native';
import { TabNavigator } from 'react-navigation';
import {Electric} from './Sub-bills/Electric';
import {Web} from './Sub-bills/WebBill';
import {Water} from './Sub-bills/Water';
import {OtherBills} from './Sub-bills/OtherBills';
import {Personal} from './Sub-bills/Personal';
export const Tabs = TabNavigator(
{
Electric: {
screen: Electric,
navigationOptions: {
tabBarLabel: 'Electric'
}
},
Web: {
screen: Web,
navigationOptions: {
tabBarLabel: 'Web'
}
},
Water: {
screen: Water,
navigationOptions: {
tabBarLabel: 'Water'
}
},
OtherBills: {
screen: OtherBills,
navigationOptions: {
tabBarLabel: 'Other'
}
},
Personal: {
screen: Personal,
navigationOptions: {
tabBarLabel: 'Personal'
}
}
},
);
export class HouseholdBills extends React.Component{
render(){
return (
<Tabs />
);
}
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
backgroundColor:'transparent',
justifyContent: 'center',
alignItems: 'center',
position: 'absolute'
},
});
A component is rendered by pressing a button
In new version of ReactNavigation putting TabNavigator under a SafeAreaView will cause it not to show, since react navigation is already handling it,
If in your case you are using SafeAreaView component on top of TabNavigator, maybe removing it will help you.
I use the following as extra configuration to the Tab Nav. You possibly can strip some stuff out, but what worked was to at least define the order.
import { TabNavigator, TabBarBottom } from 'react-navigation';
export const Tabs = TabNavigator(
{
... Your tabs here...
}
{
tabBarOptions: {
activeTintColor: 'red',
inactiveTintColor: 'grey',
style: {
backgroundColor: 'white',
borderTopColor: 'red',
},
labelStyle: {
fontSize: 12,
fontWeight: 'normal'
},
indicatorStyle: {
borderBottomColor: 'red,
borderBottomWidth: 4,
},
},
initialRouteName: 'Electric',
order: ['Electric', 'Web', 'Water', 'OtherBills', 'Personal'],
tabBarComponent: TabBarBottom,
tabBarPosition: 'bottom',
},
{
...TabNavigator.Presets,
animationEnabled: false,
swipeEnabled: false,
showIcon: false,
}
};
Related
I am trying to implement drawer navigator in my react native application. I have used the openDrawer() function in the component that should open the drawer. However on clicking the component the drawer is directly launching the screen inside the drawer rather than simply opening the drawer. Is the way i am using the navigator wrong? Any help would be appreciated.
Navigator.js
import React from 'react';
import { createAppContainer,createSwitchNavigator} from 'react-navigation';
import {createDrawerNavigator } from 'react-navigation-drawer';
import { createStackNavigator } from 'react-navigation-stack';
import {View,Platform,StatusBar} from 'react-native';
import LoginForm from './Components/LoginForm';
import TheatreList from './Components/TheatreList';
import Menu from './Components/Menu';
import EmployeeEdit from './Components/EmployeeEdit';
import CartScreen from './Components/CartScreen';
import Screen from './Components/Screen';
import ShoppingCartIcon from './Components/ShoppingCartIcon';
import PaymentScreen from './Components/PaymentScreen';
import Screen1 from './Screen/Screen1';
import Screen2 from './Screen/Screen2';
import * as Expo from 'expo';
const Navigator = createStackNavigator({
TheatreList:{
screen:TheatreList
} ,
Login: {
screen:LoginForm
},
Menu: {
screen:Menu
},
Cart: {
screen:CartScreen
},
PaymentScreen:{
screen:PaymentScreen
},
},
{
headerMode: 'none',
navigationOptions: {
header: null,
},
},
{
initialRouteName: 'TheatreList'
}
);
const AppDrawerNavigator=createDrawerNavigator(
{
Navigator,
Screen1:{
screen:Screen1
},
Screen2:{
screen:Screen2
}
});
const RootNav=createSwitchNavigator({
Navigator,
AppDrawerNavigator
});
export default createAppContainer(RootNav);
ProfileIcon.js
import React, { Component } from 'react';
import { View, Text, Picker,Button } from 'react-native';
import { connect } from 'react-redux';
import Icon from 'react-native-vector-icons/Entypo';
import { Actions } from 'react-native-router-flux';
import { DrawerActions } from 'react-navigation-drawer';
const ProfileIcon = (props) => (
<View style ={{
position:'absolute',
top:0,
left:0,
right:5,
// padding:7,
width: 40,
height: 40,
borderRadius: 40/2,
border: '1px solid #c45653',
justifyContent: 'center',
marginLeft: 15,padding:7}}>
<Icon name="menu" onPress={() => props.navi.dispatch(DrawerActions.openDrawer())} style ={{fontSize:50, position:'absolute', top:0, right:0,width:40, color:'#fe003a'}} />
</View>
)
export default ProfileIcon;
You need to make nested stacknavigators. Inside AppDrawerNavigator ,call your Navigator stack
Try This
const RootNavigator = createStackNavigator({
DrawerMenu: { screen: DrawerMenu },
Home: { screen: Home },
},
{
initialRouteName: 'Home',
navigationOptions: ({ navigation }) => ({
})
});
const Drawer = createDrawerNavigator({
RootNavigator: {
screen: RootNavigator, //rootNavigator
navigationOptions: { title: 'titleHere' },
},
},
{
initialRouteName: 'RootNavigator',
drawerPosition: 'Left',
backBehavior: 'none',
contentComponent: props => <DrawerMenu {...props} />,
drawerOpenRoute: 'DrawerOpen',
},
);
const LoginNavigator = createStackNavigator({
Login: {
screen: Login,
navigationOptions: {
drawerLockMode: 'locked-closed',
headerStyle: { },
headerTitleStyle: { },
title: 'Title'
}
},
})
export default createSwitchNavigator({
AuthLoading: AuthLoadingScreen, //SplashScreen
LoginNavigator: LoginNavigator,
Drawer: Drawer,
},
{
initialRouteName: 'AuthLoading',
})
I use react navigation to handle my app navigate. I have one problem. I have 5 navigation file. I have createMaterialTopTabNavigator that use it on StackNavigator. but I cant navigation between them.
my createMaterialTopTabNavigator
import React, { Component } from 'react';
import {
StyleSheet,
View,
Dimensions,
} from 'react-native';
import { createMaterialTopTabNavigator ,createAppContainer} from 'react-navigation';
import OneWay from '../Components/MainSearch/OneWay' ;
import TwoWay from '../Components/MainSearch/TwoWay';
import SeveralWay from '../Components/MainSearch/SeveralWay';
const FlightOptions = createMaterialTopTabNavigator({
'چندمقصده':{
screen: SeveralWay,
navigationOptions:{
header: null, headerMode: 'none',swipeEnabled:true}
},
'یک طرفه':{
screen: OneWay,
navigationOptions:{
header: null, headerMode: 'none',swipeEnabled:true}
},
'رفت و برگشت':{
screen: TwoWay,
navigationOptions:{
header: null, headerMode: 'none',swipeEnabled:true}
}
},
{
tabBarOptions: {
labelStyle: {
fontSize: 15,
color: '#15479F',
fontFamily : 'Sahel-Bold',
textDecorationLine: 'underline',
},
style: {
shadowOpacity: 0,
shadowOffset: {
height: 0,
},
shadowRadius: 0,
elevation: 0,
backgroundColor: '#fff',
},
tabStyle:{
},
indicatorStyle :{
opacity: 0
}
}
},
{
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
}
);
export default createAppContainer(FlightOptions);
and my stackNavigator:
import React, { Component } from 'react';
import {
StyleSheet,
View,
} from 'react-native';
import { createStackNavigator ,createAppContainer} from 'react-navigation';
import Calendar from '../Components/MainSearch/Calendar';
import AirLineSearchResult from '../Components/MainSearch/AirLineSearchResult';
import OneWay from '../Components/MainSearch/OneWay' ;
import TwoWay from '../Components/MainSearch/TwoWay';
import SeveralWay from '../Components/MainSearch/SeveralWay';
const Detail = createStackNavigator(
{
OneWay:{
screen : OneWay,
navigationOptions: {
header: null, headerMode: 'none'
}
},
TwoWay:{
screen : TwoWay,
navigationOptions: {
header: null, headerMode: 'none'
}
},
SeveralWay:{
screen : SeveralWay,
navigationOptions: {
header: null, headerMode: 'none'
}
},
Calendar:{
screen : Calendar,
navigationOptions: {
header: null, headerMode: 'none'
}
},
AirLineSearchResult:{
screen : AirLineSearchResult,
navigationOptions: {
header: null, headerMode: 'none'
}
},
},
{
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
}
);
export default createAppContainer(Detail);
my main navigation on app:
import { createStackNavigator, createAppContainer } from 'react-navigation';
import Splash from '../Components/Splash';
import WalkThrough from '../Components/WalkThrough';
import Login from '../Components/Login/Login';
import Validation from '../Components/Login/Validation';
import MainSearchNavigation from './MainSearchNavigation';
import OneWay from '../Components/MainSearch/OneWay';
import AirLineNavigation from './AirLineNavigation';
import HotelSearchNavigation from './HotelSearchNavigation';
const AppNavigator = createStackNavigator(
{
Splash: Splash,
WalkThrough: WalkThrough,
Login : Login,
Validation : Validation,
OneWay : OneWay,
MainSearchNavigation :{
screen : MainSearchNavigation,
navigationOptions: {
header: null, headerMode: 'none'
}
},
AirLineNavigation:{
screen :AirLineNavigation,
navigationOptions: {
header: null, headerMode: 'none'
}
},
HotelSearchNavigation:{
screen :HotelSearchNavigation,
navigationOptions: {
header: null, headerMode: 'none'
}
},
},
{
initialRouteName: "Splash"
},
{
headerMode: 'none',
navigationOptions: {
headerVisible: false,
}
}
);
export default createAppContainer(AppNavigator);
and the code of OneWay that didnt work.
<TouchableOpacity
style={styles.buttonText}
onPress={()=>this.props.navigation.navigate('AirLineSearchResult')}
>
<View style={styles.buttonContainer}>
<Text
style={styles.okBotton}
>جست و جو</Text>
</View>
</TouchableOpacity>
You have more than one StackNavigator in your app. You are trying to navigate to a screen that it's in a different Stack. Therefore you should pass also the name of the stack. Assuming that you have a main StackNavigator that you are exporting to your app.js, your would have something like this:
-Main Navigator
-ChildNavigator1
-Screen1
-Screen2
-ChildNavigator2
-Screen3
-Screen4
Then, if you are on Screen4 and want to navigate to Screen1:
this.props.navigation.navigate('ChildNavigator1', {}, NavigationActions.navigate({ routeName: 'Screen1' }))
Also dont forget to import NavigationActions from react-navigation.
export default DrawerNavigator({
MainScreen: {
screen: MainScreen,
},
CreateItem:{
screen: CreateItem,
},
MyitemScreen: {
screen: MyitemScreen,
},
Settings: {
screen: Settings,
},
Buying: {
screen: Buying,
},
Messages: {
screen: Messages,
},
Notifications: {
screen: Notifications,
}, Profile: {
screen: Profile,
}, Logout: {
screen: Logout,
},
},
{
drawerPosition:'left',
initialRouteName:'MainScreen',
drawerBackgroundColor:'white',
drawerWidth:250,
});
then
export default class MainScreen extends Component {
static navigationOptions =
{
title:'Home',
headerMode:"float",
headerStyle: {
backgroundColor: 'green',
elevation: null
},
headerTitleStyle: {
fontWeight: '300',
color: '#ffffff',
fontSize: 20,
flex:1,
textAlign:"center"
},
};
background header of mainscreen changed to green but still white color is appearing in the header
I assuming you are using 'React-Navigation'
try this prop for your static navigationOptions
static navigationsOptions = ({navigation}) = {
return{
title : 'My title',
headerStyle = {background : 'green'
}
}
I could be misunderstanding your questions btw. Posting an image might make it more clear.
I am using stack navigator inside the drawer navigator. What I want to do is, I need to know the activeItem (the active screen), so as to display it as active.
StackNavigator
const stackNav = StackNavigator({
homeComponent: { screen: HomeScreen },
serviceScreen: { screen: ServiceScreen },
serviceDetailScreen: { screen: ServiceDetailScreen },
selectVehicleScreen: { screen: SelectVehileScreen },
addEditVehicle: { screen: AddVehicle },
dateTimeScreen: { screen: DateTimeScreen },
reviewScreen: { screen: ReviewScreen },
notesScreen: { screen: NotesScreen },
}, {
headerMode: 'none'
});
DrawerNavigator
const DrawerStack = DrawerNavigator({
appointment: {
screen: stackNav,
},
}, {
headerMode: 'none',
gesturesEnabled: false,
contentComponent: DrawerContainer
});
export default DrawerStack;
What you can do is
In your context there is only one screen that can be active and that is appointment screen.
If you want to know that if appointment screen is focused then you should check the props inside the DrawerContainer Component. It will give you the activeItemKey i.e appointment.
And then you can simply check in DrawerComponent that if
this.props.activeItemKey === 'appointment' ? { color: '#000' } : { color: '#fff' }]}
You can also pass the activeTintColor prop from DrawerNavigator as shown below
You can find other DrawerNavigatorConfigs here
const DrawerStack = DrawerNavigator({
appointment: {
screen: stackNav,
},
}, {
headerMode: 'none',
gesturesEnabled: false,
contentComponent: DrawerContainer,
contentOptions: {
activeTintColor: '#e91e63',
itemsContainerStyle: {
marginVertical: 0,
},
iconContainerStyle: {
opacity: 1
}
}
});
export default DrawerStack;
I have a navigation.js which returns SwitchNavigator...
I want to put TabIcon in one of the screens....But i can't do it inside the screen,
i.e
static navigationOptions = () => ({
header: null,
tabBarLabel: 'Orders',
});
As i have a nested tab, so i don't have a screen where i can do this ....here is my code
navigation.js
const AppStack = TabNavigator({
//ORDER TAB----->>>>
orders: {
navigationOptions: {
tabBarLabel: 'Orders',
},
screen: TabNavigator({
navigationOptions: {
tabBarLabel: 'Orders',
tabBarIcon: ({ tintColor }) => {
return <Icon name="shopping-cart" size={20} color={tintColor} />;
}
},
awaitingScreen: {
screen: StackNavigator({
awaiting: { screen: AwaitingScreen },
awaitingorderdetail: { screen: AwaitingDetailScreen }
})
},
confirmedscreen: {
screen: StackNavigator({
confirmed: { screen: ConfirmedScreen },
confirmedorderdetail: { screen: ConfirmedDetailScreen }
})
},
}, {
tabBarOptions: {
activeTintColor: '#415041', // Color o f tab when pressed
inactiveTintColor: '#b5b5b5', // Color of tab when not pressed
showIcon: 'true', // Shows an icon for both iOS and Android
//showLabel: (Platform.OS !== 'android'), //No label for Android
lazy: true,
animationEnabled: false,
indicatorStyle: {
borderBottomColor: '#f5e9dd',
borderBottomWidth: 2,
},
labelStyle: {
fontSize: 10,
color: 'white',
fontFamily: 'NeutraText-Book'
},
style: {
backgroundColor: '#415041',
}
}
}
)
},
//PAYMENT TAB---->>>
payments: { screen: PaymentScreen },
//CUSTOMER TAB----->>
customers: { screen: CustomerScreen },
//MESSAGES TAB----->>
messages: { screen: MessageScreen },
//backBehavior: 'none',
},
{
headerMode: 'none', // I don't want a NavBar at top
tabBarPosition: 'bottom', // So your Android tabs go bottom
swipeEnabled: false,
lazy: true,
animationEnabled: false,
tabBarOptions: {
activeTintColor: '#415041', // Color o f tab when pressed
inactiveTintColor: '#b5b5b5', // Color of tab when not pressed
showIcon: 'true', // Shows an icon for both iOS and Android
//showLabel: (Platform.OS !== 'android'), //No label for Android
labelStyle: {
fontSize: 8,
margin: 0,
padding: 0,
fontFamily: 'NeutraText-Book'
},
indicatorStyle: {
borderBottomColor: '#f5e9dd',
borderBottomWidth: 2,
},
style: {
backgroundColor: '#fff', // Makes Android tab bar white instead of standard blue
}
}
});
const AuthStack = TabNavigator({
selectadmin: { screen: SelectAdminScreen },
otp: { screen: OtpScreen },
password: { screen: PasswordScreen },
pin: { screen: PinScreen }
},
{
navigationOptions: {
tabBarVisible: false
},
tabBarPosition: 'bottom',
swipeEnabled: false,
lazy: true,
animationEnabled: false,
//backBehavior: 'none',
}
);
export default SwitchNavigator(
{
AuthLoading: AppLoadingScreen,
App: AppStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
Now i want to import this navigator inside App.js
like
export default class App extends React.Component {
render() {
return (
<View style={{ flex: 1 }}>
<SwitchNavigator />
</View>
);
}
}
But i can't do this as i can't return JSX without React,
So is there any work around? I can declare all this inside my App.js but That is my last option as it will make my code messy.
return <Icon name="shopping-cart" size={20} color={tintColor}
It returns React must be in scope
Make sure to always import React in every file that contains jsx:
import React from 'react';
The error indicates that you forgot to do so in a file that contains jsx.