React-native-paper: drawer sample code?

Created on 13 Dec 2018  Â·  11Comments  Â·  Source: callstack/react-native-paper

There's a Drawer.Item and Drawer.Section .

But I don't get how to show or hide drawer itself.. There's no sample for drawer in the example directory either.

https://github.com/callstack/react-native-paper/issues/358 mentions removing Drawer component,
so how do we show the drawer?

Most helpful comment

Since this issue is in the first google results, here a sample code for the beginners:

App.js:

import React from "react";
import { Text, View, ScrollView } from "react-native";
import { createAppContainer, createDrawerNavigator } from "react-navigation";
import { DrawerActions } from "react-navigation";
import { Appbar, Drawer } from "react-native-paper";
import { SafeAreaView } from "react-navigation"; // âš  you need the package 'react-navigation'

class First extends React.Component {
  render() {
    return (
      <View>
        <Appbar.Header>
          <Appbar.Action
            icon="menu"
            onPress={() =>
              this.props.navigation.dispatch(DrawerActions.toggleDrawer())
            }
          />
          <Appbar.Content title="First Page" />
        </Appbar.Header>
        <Text>First Page</Text>
      </View>
    );
  }
}

class Second extends React.Component {
  render() {
    return (
      <View>
        <Appbar.Header>
          <Appbar.Action
            icon="menu"
            onPress={() =>
              this.props.navigation.dispatch(DrawerActions.toggleDrawer())
            }
          />
          <Appbar.Content title="Second Page" />
        </Appbar.Header>
        <Text>Second Page</Text>
      </View>
    );
  }
}

const Menu = createDrawerNavigator(
  {
    First: { screen: First },
    Second: { screen: Second }
  },
  {
    contentComponent: props => (
      <ScrollView>
        <SafeAreaView forceInset={{ top: "always", horizontal: "never" }}>
          <Drawer.Item
            label="First Page"
            active="true"
            onPress={() => props.navigation.navigate("First")}
          />
          <Drawer.Item
            label="Second Page"
            active="true"
            onPress={() => props.navigation.navigate("Second")}
          />
        </SafeAreaView>
      </ScrollView>
    )
  }
);

const AppNav = createAppContainer(Menu);

export default class App extends React.Component {
  render() {
    return <AppNav />;
  }
}

All 11 comments

Also curious about this, surprised to not see a drawer component, is there an well supported alternative?

The doc is unclear on how to put a Drawer.

@hotgeart Our library supports Drawer.Item and Drawer.Section components which are just building blocks that you can put inside drawer to make it look like material component. If you need drawer component take a look at react-navigation which is a navigation library for react-native and it provides Drawer component (we also use it in our example app).

Since this issue is in the first google results, here a sample code for the beginners:

App.js:

import React from "react";
import { Text, View, ScrollView } from "react-native";
import { createAppContainer, createDrawerNavigator } from "react-navigation";
import { DrawerActions } from "react-navigation";
import { Appbar, Drawer } from "react-native-paper";
import { SafeAreaView } from "react-navigation"; // âš  you need the package 'react-navigation'

class First extends React.Component {
  render() {
    return (
      <View>
        <Appbar.Header>
          <Appbar.Action
            icon="menu"
            onPress={() =>
              this.props.navigation.dispatch(DrawerActions.toggleDrawer())
            }
          />
          <Appbar.Content title="First Page" />
        </Appbar.Header>
        <Text>First Page</Text>
      </View>
    );
  }
}

class Second extends React.Component {
  render() {
    return (
      <View>
        <Appbar.Header>
          <Appbar.Action
            icon="menu"
            onPress={() =>
              this.props.navigation.dispatch(DrawerActions.toggleDrawer())
            }
          />
          <Appbar.Content title="Second Page" />
        </Appbar.Header>
        <Text>Second Page</Text>
      </View>
    );
  }
}

const Menu = createDrawerNavigator(
  {
    First: { screen: First },
    Second: { screen: Second }
  },
  {
    contentComponent: props => (
      <ScrollView>
        <SafeAreaView forceInset={{ top: "always", horizontal: "never" }}>
          <Drawer.Item
            label="First Page"
            active="true"
            onPress={() => props.navigation.navigate("First")}
          />
          <Drawer.Item
            label="Second Page"
            active="true"
            onPress={() => props.navigation.navigate("Second")}
          />
        </SafeAreaView>
      </ScrollView>
    )
  }
);

const AppNav = createAppContainer(Menu);

export default class App extends React.Component {
  render() {
    return <AppNav />;
  }
}

snack of @hotgeart's example code if you want to see it in action:

https://snack.expo.io/@natelowry/react-native-paper-drawer-sample

Would it be possible to see an implementation example of a Drawer Item with a Badge ? Tried everything and wasn't able to add one :/

Am curious if there is any examples out there without relying on react-navigation? React Navigation is a GREAT library but would love to know if there is a reasonable way to implement material style navigation without it.

I've gotten the drawer implemented, but the only way currently to dismiss it is by clicking the android back button, would it be possible to click on the screen area next to the drawer to dismiss, by screen area i mean the area that is darkened, though which you can see the base screen, as the drawer only takes up 75% of the screen. Any insight would be appreciated.

@xXFracXx I ended up setting things up with React Gesture Handler Drawer Layout. I am facing the same issue and my approach will be to use the Tap Gesture Handler, similar to Material UI's click away listener: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/ClickAwayListener/ClickAwayListener.js

Same. I'm using react-router-native. I ended up using react-native-side-menu-updated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tonyxiao picture tonyxiao  Â·  3Comments

zxccvvv picture zxccvvv  Â·  4Comments

knobandre picture knobandre  Â·  4Comments

ButuzGOL picture ButuzGOL  Â·  3Comments

talaikis picture talaikis  Â·  3Comments