【发布时间】:2019-08-07 12:00:47
【问题描述】:
这是我使用反应导航的导航堆栈v3.2.1:
我有一个切换导航器来切换到身份验证导航堆栈和经过身份验证的应用堆栈。
App 堆栈是使用底部选项卡导航器制作的。
我想为选项卡导航器使用自定义组件。
在使用createBottomTabNavigator 和自定义tabBarComponent 时如何获取选项卡导航器的当前路由名称。
例如:
- 假设标签导航堆栈有 2 个导航屏幕,即主页和聊天。
- 在自定义BottomBar中,如何查看focus/active/current routeName是否为Home/Chat,以便分别更改图标样式?
AppContainer.js
const switchStack = createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack
}, {
initialRouteName: 'AuthLoading',
})
export default createAppContainer(switchStack)
AppStack.js
const AppStack = createBottomTabNavigator({
Home: {
screen: HomeStack,
},
Chat: {
screen: ChatStack
},
}, {
initialRouteName: 'Home',
activeColor: '#f0edf6',
inactiveColor: '#3e2465',
shifting: false,
barStyle: {
backgroundColor: '#694fad',
},
labeled: false,
tabBarComponent: ({navigation}) => <BottomBar navigation={navigation}/>
})
export default AppStack
BottomBar.js
export default class BottomBar extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<View style={styles.container}>
<IconComponent routeName={'Home'}/>
<IconComponent routeName={'Chat'}/>
</View>
)
}
}
图标组件.js
export default class IconComponent extends React.Component {
constructor(props) {
super(props)
}
...
render() {
let IconComponent
let iconName
let iconSize = 25
switch (this.props.routeName) {
case 'Home':
IconComponent = MaterialCommunityIcons
// iconName = `home${focused ? '' : '-outline'}`;
iconName = `home`;
break
case 'Chat':
IconComponent = AntDesign
iconName = `message1`
iconSize = 22
break
}
let tintColor = 'green'
// if focused Home is current tab screen then change style eg. tint color.
// similary if current tab screen is Chat, then change style.
return (
<Animated.View
style={[
styles.container,
{
opacity: this.opacity
}
]}
>
<IconComponent name={iconName} size={iconSize} color={tintColor}/>
</Animated.View>
)
}
}
【问题讨论】:
标签: reactjs react-native react-navigation