简体   繁体   English

React-native DrawerNavigator不会显示

[英]React-native DrawerNavigator does not show up

I am trying to create simple DrawerNavigator. 我正在尝试创建简单的DrawerNavigator。 But it does not show up :( 但它没有出现:(

import React, { Component } from 'react';
import { Text } from 'react-native';
import { createAppContainer, createDrawerNavigator } from 'react-navigation';

class One extends Component {
    render() {
        return (<Text>One</Text>);
    }
}

class Two extends Component {
    render() {
        return (<Text>Two</Text>);
    }
}

const DrawerStack = createDrawerNavigator(
    {
        One: {screen: One},
        Two: {screen: Two},
    }, {
    }
);

const App = createAppContainer(DrawerStack);
export default App;

This is what I see (no drawer navigation is displayed): 这是我看到的(没有显示抽屉导航):

没有抽屉导航仪

  1. Swipe from the left and you will see the drawerNavigation works 从左侧滑动,您将看到drawerNavigation正常工作

  2. the react-navigation createDrawerNavigator does not provide stack navigation by default, if you are looking to see a header with the menu icon, then you have to make your screens (one, two) be stackNavigation. react-navigation createDrawerNavigator默认情况下不提供堆栈导航,如果您要查看带有菜单图标的标题,则必须使您的屏幕(一,二)为stackNavigation。

    NB: import Icon from expo or react-native-icons 注意:从expo或react-native-icons导入图标

Updated:: using native-base 更新::使用native-base

  1. install native-base ( ** npm install native-base --save** ) 安装native-base(** npm install native-base --save **)
  2. import Header, Icon, Container, Left, Content from native-base 导入标题,图标,容器,左,内容来自本机

    class One extends Component { 第一类扩展组件{

     render() { return ( <Container> <Header> <Left> <Icon name="md-menu" onPress={() => this.props.navigation.openDrawer()} /> </Left> </Header> <Content> <Text>Screen One</Text> </Content> </Container> ); } 

    } }

    class Two extends Component { 第二类扩展Component {

     render() { return ( <Container> <Header> <Left> <Icon name="md-menu" onPress={() => this.props.navigation.openDrawer()} /> </Left> </Header> <Content> <Text>Screen Two</Text> </Content> </Container> ); } 

    } }

    const DrawerStack = createDrawerNavigator( { one:{screen:One}, two: {screen: Two} }, { const DrawerStack = createDrawerNavigator({one:{screen:One},two:{screen:Two}},{

    } ); });

抽屉 屏幕一 屏幕二

Drawer Swipe Gesture is not working because in the new version of react navigation v3 npm. 抽屉滑动手势不起作用,因为在新版本的反应导航v3 npm。 We need to install react-native-gesture-handler npm separately. 我们需要分别安装react-native-gesture-handler npm。 They create separated npm package for touch & gesture handling and recognition. 他们创建了分离的npm包,用于触摸和手势处理和识别。

Step 1. 步骤1。

npm i react-native-gesture-handler

Step 2. 第2步。

react-native link react-native-gesture-handler

Step 3.(optional ) 第3步。(可选)

If step 2 is not worked properly, code is confogured properly 如果步骤2无法正常工作,则代码会被正确配置

To finalize the installation of react-native-gesture-handler for Android, be sure to make the necessary modifications to MainActivity.java: 要完成针对Android的react-native-gesture-handler的安装,请务必对MainActivity.java进行必要的修改:

package com.reactnavigation.example;

import com.facebook.react.ReactActivity;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "Example";
  }

+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected ReactRootView createRootView() {
+       return new RNGestureHandlerEnabledRootView(MainActivity.this);
+      }
+    };
+  }
}

No additional steps are required for iOS. iOS无需其他步骤。

Please Refer the following document for more information:- 有关更多信息,请参阅以下文档: -

  1. https://reactnavigation.org/docs/en/getting-started.html#installation https://reactnavigation.org/docs/en/getting-started.html#installation

  2. https://www.npmjs.com/package/react-native-gesture-handler/v/1.0.0-alpha.34?activeTab=readme https://www.npmjs.com/package/react-native-gesture-handler/v/1.0.0-alpha.34?activeTab=readme

  3. https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html

It's good to take advantage of the native base. 利用原生基地是很好的。

The relevant link is here. 相关链接在这里。

The navigation links are here. 导航链接在这里。

You can also see the component here, so you can use the features you need. 您还可以在此处查看组件,以便使用所需的功能。

Drawer navigation example.js: 抽屉导航example.js:

import React, { Component } from "react";
import HomeScreen from "./HomeScreen.js";
import MainScreenNavigator from "../ChatScreen/index.js";
import Profile from "../ProfileScreen/index.js";
import SideBar from "../SideBar/SideBar.js";
import { DrawerNavigator } from "react-navigation";
const HomeScreenRouter = DrawerNavigator(
  {
    Home: { screen: HomeScreen },
    Chat: { screen: MainScreenNavigator },
    Profile: { screen: Profile }
  },
  {
    contentComponent: props => <SideBar {...props} />
  }
);
export default HomeScreenRouter;

在此输入图像描述

Easy to operate without special implementation. 无需特殊实施即可轻松操作。

If you have any further comments, please leave a comment. 如果您有任何进一步的意见,请发表评论。 And if my answer is good, please make a choice. 如果我的答案很好,请做出选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM