【发布时间】:2017-05-18 16:30:01
【问题描述】:
最近我一直在尝试使用react-navigation 进行导航布局。我有 4 个选项卡,它们嵌套在抽屉导航中。我有 3 个抽屉标签。我想有条件地渲染一个标签的页面,假设 Home Tab 根据活动抽屉标签。
我的代码如下:
Screens.js
import React from 'react';
import { View, Text } from 'react-native';
export const LaptopHome = () => {
return(
<View style={{flex: 1, backgroundColor: '#e3e3e3'}}>
<Text>
This is laptop Home Page.
</Text>
</View>
);
}
export const CameraHome = () => {
return(
<View style={{flex: 1, backgroundColor: 'skyBlue'}} >
<Text>This is Camera Home Page</Text>
</View>
)
}
export const MobileHome = () => {
return(
<View style={{flex: 1}}>
<Text>This is a Mobile Home page</Text>
</View>
);
}
export const Orders = () => {
return(
<View style={{flex: 1}}>
<Text>Orders history details</Text>
</View>
);
}
export const Profile = () => {
return(
<View style={{flex: 1}}>
<Text>Profile details</Text>
</View>
);
}
export const Notification = () => {
return(
<View style={{flex: 1}}>
<Text>List of Notification</Text>
</View>
);
}
Routes.js
import { StackNavigator, TabNavigator } from 'react-navigation';
import { FontAwesome } from '@expo/vector-icons';
import { LaptopHome, CameraHome, MobileHome, Orders, Profile, Notification } from './Screens';
const HomeStack = StackNavigator({
Home: {
screen: LaptopHome //or CameraHome or MobileHome //Render according to active drawer label.
}
});
const OrdersStack = StackNavigator({
Order: {
screen: Orders,
}
});
const ProfileStack = StackNavigator({
Profile: {
screen: Profile,
}
});
const NotificationStack = StackNavigator({
Notification: {
screen: Notification,
}
});
const HomeTab = TabNavigator({
HomeTab: {
screen: HomeStack,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor }) => <FontAwesome name="home" size={24} color={tintColor} />,
},
},
OrderTab: {
screen: OrdersStack,
navigationOptions: {
tabBarLabel: 'Orders',
tabBarIcon: ({ tintColor }) => <FontAwesome name="cart-arrow-down" size={24} color={tintColor} />
},
},
ProfileTab: {
screen: ProfileStack,
navigationOptions: {
tabBarLabel: 'Profile',
tabBarIcon: ({ tintColor }) => <FontAwesome name="user" size={24} color={tintColor} />
},
},
NotificationTab: {
screen: NotificationStack,
navigationOptions: {
tabBarLabel: 'Notification',
tabBarIcon: ({ tintColor }) => <FontAwesome name="bell" size={24} color={tintColor} />
},
}
}, {
initialRouteName: 'HomeTab',
tabBarPosition: 'bottom',
swipeEnabled: false,
tabBarOptions: {
activeTintColor: '#4DD1A5',
inactiveTintColor: '#AAADAC',
upperCaseLabel: false,
showIcon: true,
style: {
backgroundColor: '#fff',
},
labelStyle:{
fontSize: 10,
margin: 0,
},
indicatorStyle: {
opacity: 0,
},
},
});
export const AppNavigator = DrawerNavigator({
Laptop: {
screen: HomeTab,
navigationOptions: {
drawerLabel: 'Laptop',
}
},
Camera: {
screen: HomeTab,
navigationOptions: {
drawerLabel: 'Camera',
}
},
Mobile: {
screen: HomeTab,
navigationOptions: {
drawerLabel: 'Mobile',
drawerIcon: ({ tintColor }) => (
<MaterialCommunityIcons name="table-large" size={24} color={tintColor} />
),
}
}
}, {
initialRouteName: 'Laptop',
contentOptions: {
activeTintColor: '#4DD1A5',
inactiveTintColor: '#AAADAC',
activeBackgroundColor: '#ffffff',
}
});
App.js
'use strict';
import React, { Component } from 'react';
import {AppNavigator} from './Routes';
export default class App extends Component {
render() {
return (
<AppNavigator />
);
}
}
AppRegistry.registerComponent('SimpleApp', () => App);
所以,我想根据活动抽屉标签有条件地呈现 HOME 选项卡。 我尝试通过访问反应导航的状态,但它没有帮助。有没有人解决这个问题或者我错过了什么。
【问题讨论】:
标签: javascript android reactjs react-native react-navigation