Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting Navigators with React Navigators - 'Home' should declare a screen

I am currently working with React Native and the new React Navigation. There I'm trying to follow the tutorial for nesting navigators, but there is always an error:

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

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

I'm not sure what I'm doing wrong. When I start the navigators individually, they work. But unfortunately not combined.

This is the App.js, which I have created using the tutorial:

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

Maybe you can help me there. I asked the same question here, but maybe this is the better place for the question.

Thanks in advance!

like image 690
Daniel Avatar asked Mar 04 '17 13:03

Daniel


People also ask

How do I navigate to nested screen in react navigation?

If the navigator was already rendered, navigating to another screen will push a new screen in case of stack navigator. In the above case, you're navigating to the Media screen, which is in a navigator nested inside the Sound screen, which is in a navigator nested inside the Settings screen.

Does React Native allows for nesting of the navigation stacks?

Developing a simple React Native application When you nest navigators, the screens of one navigator are rendered inside the screens of another. If there is a stack, switching to a different screen will cause a new screen to be displayed. Navigators are in charge of making the switch between different screens.


1 Answers

I made this mistake when I was starting out as well...

Put MainScreenNavigator above ReactNativeTest.

As you currently have it, MainScreenNavigator doesn't exist when it's called.

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

const ReactNativeTest = StackNavigator({
    Home: { screen: MainScreenNavigator },
    Chat: { screen: ChatScreen },
});
like image 174
GollyJer Avatar answered Oct 24 '22 21:10

GollyJer