react-native - 一个 React Native 屏幕可以包含在两个不同的堆栈导航器中吗?

标签 react-native react-navigation stack-navigator react-native-tabnavigator

是否有任何理由不应将屏幕包含在两个不同的堆栈导航器中?

我希望它在两个堆栈导航器中都有一个平滑的、流入的页面过渡和后退按钮,以平滑地返回上一页。在下面的示例中,ContactScreen出现两次:

const StackOne = createStackNavigator({
  About: AboutScreen,
  FAQ: FAQScreen,
  Contact: ContactScreen
});

const StackTwo = createStackNavigator({
  Main: MainScreen,
  Details: DetailsScreen
  Contact: ContactScreen
});

const MainTabs = createBottomTabNavigator({
  TabOne: StackOne,
  TabTwo: StackTwo
});

最佳答案

是的,您可以在不同的堆栈导航器中使用相同的屏幕。按照这个例子:

.js file


import React from 'react';
import { Button, Text, View } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { createStackNavigator, createBottomTabNavigator, createAppContainer } from 'react-navigation';

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

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

class DetailsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Details!</Text>
      </View>
    );
  }
}

const HomeStack = createStackNavigator({
  Home: { screen: HomeScreen },
  Details: { screen: DetailsScreen },
});

const SettingsStack = createStackNavigator({
  Settings: { screen: SettingsScreen },
  Details: { screen: DetailsScreen },
});

export default createAppContainer(createBottomTabNavigator(
  {
    Home: { screen: HomeStack },
    Settings: { screen: SettingsStack },
  },
  {
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, tintColor }) => {
        const { routeName } = navigation.state;
        let iconName;
        if (routeName === 'Home') {
          iconName = `ios-information-circle${focused ? '' : '-outline'}`;
        } else if (routeName === 'Settings') {
          iconName = `ios-options${focused ? '' : '-outline'}`;
        }

        // You can return any component that you like here! We usually use an
        // icon component from react-native-vector-icons
        return <Ionicons name={iconName} size={25} color={tintColor} />;
      },
    }),
    tabBarOptions: {
      activeTintColor: 'tomato',
      inactiveTintColor: 'gray',
    },
  }
));

关于react-native - 一个 React Native 屏幕可以包含在两个不同的堆栈导航器中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070034/

相关文章:

react-native - 在向后滑动时执行操作( react 导航)

javascript - 如何使用 react 导航在 react native 中从登录页面导航到主页?

android - 在所有屏幕上 react native 抽屉导航器显示

react-native - 在 iOS 11 上启动屏幕后应用程序闪烁白色?

android - 应用程序启动时出现错误 "Could not get BatchedBridge, make sure your bundle is packaged properly"

android - 我可以使用 Amazon fire 平板电脑来测试我的 android react native 应用程序吗?

javascript - 滑动启用 : false option is not working

react-native - React Native 重新安装屏幕

react-native - react 导航 : Share state between two screens within TabNavigator

react-native - React Native - StackActions.reset 什么都不做