React Navigation - 如何在Tab选项卡导航器中嵌套包装堆栈导航器的组件

问题描述 投票:2回答:1

我正在尝试使用选项卡导航器,其中设置屏幕/选项卡是一个组件,除其他外,它包含一个堆栈导航器。

这是我目前的代码(也可以找到as a snack)。

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

我目前得到一个'类型错误:否'路由“在导航状态中找到”错误。您是否尝试通过React组件的导航道具

javascript react-native react-navigation
1个回答
0
投票

你需要将组件嵌入一个stackNavigator中,包含所有的stackScreen,就像这样;

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 style={{alignItems:'center', justifyContent:'center', flex:1}}>
        <Text>Screen A</Text>
      </View>
    );
  }
}

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

class SettingsScreenA extends React.Component {
  render() {
    return (
      <View style={{alignItems:'center', justifyContent:'center', flex:1}}>
        <Text>Settings A</Text>
        <Button onPress={() => this.props.navigation.navigate('SettingsHome')}
          title ="Back to SettingsHome"
        />
      </View>
    );
  }
}

class SettingsScreen extends React.Component {
  render() {
    return (
      <View style={{alignItems:'center', justifyContent:'center', flex:1}}>
        <Text>Some other components...</Text>
          <Button title="Seting A" onPress={() => this.props.navigation.navigate('SettingsScreenA')} />

      </View>
    );
  }
}

// Create our stack navigator
const SettingsStackNavigator = createStackNavigator({
  SettingsMain: {screen: SettingsScreen,},
  SettingsHome: { screen: SettingsHome },
  SettingsScreenA: { screen: SettingsScreenA }
}, {initialRouteName: 'SettingsMain'})



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

// And the app container
export default createAppContainer(RootTabNavigator);

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.