React 导航抽屉中的 React Native 显示模式

2024-05-07

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

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

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

我的模态代码是:

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 https://reactnavigation.org/docs/drawer-based-navigation

对于 React Native 导航 v6,mode="modal" 被删除以利于演示:'modal' https://reactnavigation.org/docs/6.x/upgrading-from-5.x#modemodal-is-removed-in-favor-of-presentation-modal

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React 导航抽屉中的 React Native 显示模式 的相关文章

随机推荐