【问题标题】:Can not able to navigate to child screens无法导航到子屏幕
【发布时间】:2020-05-31 20:40:30
【问题描述】:

我的初始路由名称是 loginScreen

登录后第二个屏幕带有 BottamTabNavigation。 BottomTab 包含 4 个屏幕,其中一个名为 GroupScreen 的屏幕转到 TopTabScren。我无法导航到此屏幕。

流程是 --> loginScreen --> BottomTabScreen --> TopTabScreen。

我无法导航到 TopTabScreen。它给出了错误

"undefined 不是 对象(评估'this.props.navigation.navigate' ".

TopTabScreen 导致另外 4 个屏幕

但是当我设置 InitailRoutName= "TobTabScreen" 时,所有四个屏幕都可以使用它。

对于导航屏幕,我正在使用它。

onPress={() => this.props.navigation.navigate('screenName')}.  

【问题讨论】:

  • 分享您到目前为止所做的代码示例,显示导航堆栈中的容器组件,您尝试从中访问导航的子组件!

标签: react-native react-navigation tabbar


【解决方案1】:

获取底部标签栏屏幕导航道具

_getScreenProps = () => {
    return (
        {
            navigation: this.props.navigation,
        }
    )
}

渲染标签栏

render() {
    return (
       <View style={{ flex: 1.0 }}>
            <Stack screenProps={this._getScreenProps()} />
        </View>
    )
}

在选项卡屏幕上使用如下导航

onPress={() => this.props.screenProps.navigation.navigate('screenName')}.  

标签栏

const TabView = createBottomTabNavigator({
    Home: {
        screen: Home,
    },
    Contacts: {
        screen: Contacts,
    },
    Group: {
        screen: Group,
    },
    Task: {
        screen: Task,
    },
    Event: {
        screen: EventView
    }
},
    {
        defaultNavigationOptions: ({ navigation }) => ({
            defaultProps: navigation,
            tabBarIcon: ({ focused, horizontal, tintColor }) => {
                const { routeName } = navigation.state;
                let iconName;
                let title = ''
                if (routeName === 'Home') {
                    iconName = 'ic_home'
                    title = "HOME"
                } else if (routeName === 'Contacts') {
                    iconName = 'ic_user_home'
                    title = "CONTACTS"
                } else if (routeName === 'Group') {
                    iconName = 'ic_group'
                    title = "PROSPECTS"
                } else if (routeName === 'Task') {
                    iconName = 'ic_document'
                    title = "TASKS"
                } else if (routeName === 'Event') {
                    iconName = 'ic_calculator'
                    title = "EVENTS"
                }


                if (focused) {
                    return (
                        <LinearGradient style={{ flex: 1.0, width: '100%' }}
                            colors={[Constant.COLOR.grediantTop, Constant.COLOR.grediantBottom]}>
                            <View style={{ flex: 1.0, justifyContent: 'center' }}>
                                <Image
                                    style={{
                                        height: 25,
                                        width: 25,
                                        tintColor: 'white',
                                        alignSelf: 'center'
                                    }}
                                    tintColor='white'
                                    source={{ uri: iconName }}
                                    resizeMode='contain' />
                                <SPText
                                    style={{ alignSelf: 'center', marginTop: 2, textAlign: 'center' }}
                                    fontSize={8}
                                    textColor='white'
                                    text={title} />
                            </View>
                        </LinearGradient>
                    )
                }
                else {
                    return (
                        <View style={{ flex: 1.0, justifyContent: 'center' }}>
                            <Image
                                style={{
                                    height: 25,
                                    width: 25,
                                    alignSelf: 'center'
                                }}
                                source={{ uri: iconName }}
                                resizeMode='contain' />
                            <SPText
                                style={{ alignSelf: 'center', marginTop: 2 }}
                                fontSize={8}
                                textColor='gray'
                                text={title} />
                        </View>
                    )
                }


            },
            tabBarOnPress: () => {
                const { routeName } = navigation.state;
                navigation.navigate(routeName)
            },

        }),
        tabBarOptions: {
            showLabel: false,
            inactiveTintColor: 'gray',
            inactiveBackgroundColor: 'white',
        },
    })


const RootStack = createStackNavigator(
    {
        TabView: {
            screen: TabView,
        },
    },
    {
        mode: 'modal',
        headerMode: 'none',
    }
);


const Stack = createAppContainer(RootStack);

【讨论】:

    【解决方案2】:

    从表面上看,您误解了导航的工作原理!在 react native 应用的子组件系统中。

    如果一个组件在创建时被包装成一个屏幕在导航器对象中,它将可以直接访问导航属性!

    但是如果你有一个在组件渲染中的子组件并且它不是屏幕(当然),那么你必须手动将导航作为道具传递给它!

    在我看来这根本不是一个好方法!但在你的情况下,它会起作用

    注意:如果我是正确的,请发布您的子组件代码!我会的 帮助展示示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-30
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多