【问题标题】:React Native Display Modal from React Navigation Drawer来自 React Navigation Drawer 的 React Native Display Modal
【发布时间】:2020-08-05 22:55:36
【问题描述】:

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

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;

【问题讨论】:

    标签: reactjs react-native navigation-drawer react-navigation-drawer


    【解决方案1】:

    您需要创建一个包含 Modal 组件和 Drawer Navigator 的 Root Stack Navigator。

    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'

    【讨论】:

    • 这是完美的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 2021-01-29
    • 2021-12-08
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多