来自 React Navigation Drawer 的 React Native Display Modal

React Native Display Modal from React Navigation Drawer

我有一个应用程序,我希望某些导航路线在用户单击它们时在当前页面上显示模式,而不是导航到一个完全不同的页面。我现有的代码是:

const DrawerNavigator = () => {
    return (
      <Drawer.Navigator>
        <Drawer.Screen name="My Porfolio" component={Portfolio} />
        <Drawer.Screen name="Account" component={Account} />
        <Drawer.Screen name="NBA" component = {NBALobby} />
        <Drawer.Screen name="NFL" component = {NFLLobby} />
        <Drawer.Screen name="How To Play" component = {HowToPlayModal} />
      </Drawer.Navigator>
    );
  }

我的模式按预期显示,但它似乎显示在一个新的、完全空白的页面上。有没有办法从 Drawer Navigator 调用显示模态的函数?

我的模态代码是:

import React, { Component, useState } from 'react';
import {Text, TextInput, View, TouchableOpacity} from 'react-native';
import Modal from 'react-native-modal';

import {styles} from '../styles/signUpIn';
    
const HowToPlayModal = () => {
    
    return(
        <Modal isVisible = {true}>
                <View style={styles.container2}>
                <View style={{flexDirection: 'row'}}>
                        <Text style={styles.title}>How To Play</Text>
                        <View style= {{flex:1, alignItems: 'flex-end'}}>
                                <Text style={[styles.fieldTitle, {marginLeft: 0}]}>Remember?</Text>
                                <Text style={styles.accent} >Sign In</Text>
                        </View>      
                </View>
                </View>
        </Modal>
        );
}
    
export default HowToPlayModal;

您需要创建一个包含模态组件和抽屉导航器的根堆栈导航器。

const Drawer = createDrawerNavigator();
const RootStack = createStackNavigator();

const DrawerNavigator = () => {
    return (
        <Drawer.Navigator>
            <Drawer.Screen name="My Porfolio" component={Portfolio} />
            <Drawer.Screen name="Account" component={Account} />
            <Drawer.Screen name="NBA" component = {NBALobby} />
            <Drawer.Screen name="NFL" component = {NFLLobby} />
        </Drawer.Navigator>
    );
};

const RootStackScreen = () => {
  return (
    <NavigationContainer>
        <RootStack.Navigator mode="modal">
            <RootStack.Screen
                name="Main"
                component={DrawerNavigator}
                options={{ headerShown: false }}
            />
            <RootStack.Screen name="HowToPlayModal" component={HowToPlayModal} />
        </RootStack.Navigator>
    </NavigationContainer>
  );
}

完整的解释可以在这里找到https://reactnavigation.org/docs/drawer-based-navigation

对于 React Native Navigation v6,mode="modal" is removed in favor of presentation: 'modal'