使用React Navigators嵌套导航器 - “Home”应声明一个屏幕

时间:2017-03-04 13:42:55

标签: javascript android react-native react-navigation

我目前正在使用React Native和新的React Navigation。在那里,我试图关注tutorial for nesting navigators,但始终存在错误:

Route 'Home' should declare a screen. For example:

import MyScreen from './MyScreen';
...
Home: {
   screen: MyScreen,
}

我不确定我做错了什么。当我单独启动导航器时,它们可以工作。但不幸的是没有合并。

这是我使用教程创建的App.js:

import React from 'react';
import {
    AppRegistry,
    Text,
    View,
    Button
} from 'react-native';

import {
    StackNavigator,
    TabNavigator
} from 'react-navigation';

class HomeScreen extends React.Component {
    static navigationOptions = {
        title: 'Welcome',
    };
    render() {
        const { navigate } = this.props.navigation;
        return <View>
            <Text>Hello!</Text>
            <Button
                onPress={() => this.props.navigation.navigate('Chat', { user: 'Daniel' })}
                title="ReactNavigation Test"
            />
        </View>;
    }
}

class ChatScreen extends React.Component {
    static navigationOptions = {
        title: 'ReactNavigation Test',
    };
    render() {
        const { params } = this.props.navigation.state;
        return (
            <View>
                <Text>Neue Seite für den Navigator. Hallo {params.user}!</Text>
            </View>
        );
    }
}

class RecentChatsScreen extends React.Component {
    render() {
        return <Text>List of recent chats</Text>
    }
}

class AllContactsScreen extends React.Component {
    render() {
        return <Text>List of all contacts</Text>
    }
}

const ReactNativeTest = StackNavigator({
    Home: { screen: MainScreenNavigator },
    Chat: { screen: ChatScreen },
});

const MainScreenNavigator = TabNavigator({
    Recent: { screen: RecentChatsScreen },
    All: { screen: AllContactsScreen },
});

AppRegistry.registerComponent('ReactNativeTest', () => ReactNativeTest);

也许你可以帮助我。我问同样的问题here,但也许这是问题的更好地方。

提前致谢!

1 个答案:

答案 0 :(得分:6)

我开始的时候犯了这个错误......

MainScreenNavigator置于ReactNativeTest之上。

正如您目前所拥有的那样,MainScreenNavigator在被调用时并不存在。

const MainScreenNavigator = TabNavigator({
    Recent: { screen: RecentChatsScreen },
    All: { screen: AllContactsScreen },
});

const ReactNativeTest = StackNavigator({
    Home: { screen: MainScreenNavigator },
    Chat: { screen: ChatScreen },
});