无法导航到子屏幕

时间:2020-02-16 21:56:41

标签: react-native react-navigation tabbar

我的初始RouteName是 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')}.  

enter image description here

2 个答案:

答案 0 :(得分:1)

获取Botttom标签栏屏幕导航道具

_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);

答案 1 :(得分:1)

从外观上,您对导航的工作方式产生了误解!在React Native应用的子组件系统中。

如果在创建组件时将其作为屏幕包装在导航器对象中,则可以直接访问导航道具!

但是,如果子组件位于组件的渲染内部并且不是屏幕(可以肯定),那么您必须手动将导航作为对它的支持!

在我看来,这根本不是一个好方法!但对于您而言,它将起作用

注意:如果我正确地张贴了您的子代码,则!我会的 帮助展示示例