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!
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.
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.
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 },
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With