如何将包装堆栈导航器的组件嵌套在选项卡导航器中

时间:2020-08-20 12:57:42

标签: react-native react-navigation

当前行为 我正在尝试使用一个标签导航器,其中的屏幕/选项卡之一具有一个组件,其中包括一个堆栈导航器。 但是,我目前收到“在导航状态下找不到“路线””错误。

如何复制 我当前正在运行的代码也可以作为点心。 代码:

    import * as React from 'react';
import { Button, Text, View, StyleSheet } from 'react-native';
import { createAppContainer, createStackNavigator, createBottomTabNavigator } from 'react-navigation';

class ScreenA extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'A',
  };

  render() {
    return (
      <View>
        <Text>Screen A</Text>
      </View>
    );
  }
}

class SettingsHome extends React.Component {
  render() {
    return (
      <Button onPress={() => this.props.navigation.navigate('SettingsScreenA')}>
        <Text>Navigate to Settings A</Text>
      </Button>
    );
  }
}

class SettingsScreenA extends React.Component {
  render() {
    return (
      <View>
        <Text>Settings A</Text>
        <Button onPress={() => this.props.navigation.navigate('SettingsA')}>
          <Text>Back to SettingsHome</Text>
        </Button>
      </View>
    );
  }
}

const SettingsStackNavigator = createStackNavigator({
  SettingsHome: { screen: SettingsHome },
  SettingsScreenA: { screen: SettingsScreenA }
})

class SettingsScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Settings',
  }

  render() {
    return (
      <View>
        <Text>Some other components...</Text>
        <SettingsStackNavigator navigation={this.props.navigation}/>
      </View>
    );
  }
}

const RootTabNavigator = createBottomTabNavigator({
  ScreenA: { screen: ScreenA },
  Settings: {screen: SettingsScreen },
});

const Navigation = createAppContainer(RootTabNavigator);

export default class App extends React.Component {
  render() {
    return (
      <Navigation />
    );
  }
}

1 个答案:

答案 0 :(得分:0)

在此按钮中,您要导航到SettingsA

<Button onPress={() => this.props.navigation.navigate('SettingsA')}>
          <Text>Back to SettingsHome</Text>
        </Button>

但是您将“设置”屏幕的路径定义为Settings

const RootTabNavigator = createBottomTabNavigator({
  ScreenA: { screen: ScreenA },
  Settings: {screen: SettingsScreen },
});

您必须通过在标签导航器中将SettingsA更改为路由名称来进行修复

const RootTabNavigator = createBottomTabNavigator({
  ScreenA: { screen: ScreenA },
  SettingsA: {screen: SettingsScreen },
});