react导航中获取标签导航器当前活动屏幕路径

新手上路,请多包涵

这是我使用反应导航 v3.2.1 的导航堆栈:

  1. 我有一个切换导航器来切换到身份验证导航堆栈和经过身份验证的应用程序堆栈。

  2. 应用程序堆栈是使用底部选项卡导航器制作的。

  3. 我想为选项卡导航器使用自定义组件。

使用 createBottomTabNavigator 和自定义 tabBarComponent 时,如何获取选项卡导航器的当前 routeName 。

例如:

  1. 假设选项卡导航堆栈有 2 个导航屏幕,即主页和聊天。
  2. 在自定义BottomBar中,如何检查focused/active/current routeName是否为Home/Chat,以便分别更改图标的样式?

应用容器.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>
        )
    }
}

IconComponent.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>
        )
    }
}

原文由 Kakar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 530
1 个回答

自定义 BottomBar 的导航对象有一个索引,该索引保存当前活动屏幕索引

tabBarComponent: ({navigation}) => <BottomBar navigation={navigation}/>

navigation.state.index

如果主屏幕处于活动状态 >> navigation.state.index 将为 0 如果聊天屏幕处于活动状态 >> navigation.state.index 将为 1 …等

原文由 Hend El-Sahli 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题