Fix drawer closing behavior

This commit is contained in:
Brent Vatne
2018-06-20 15:29:57 -07:00
parent 959a980087
commit 912ef8b9d7
4 changed files with 15 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
import * as React from 'react';
import Expo from 'expo';
import { FlatList } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import { createSwitchNavigator, createStackNavigator } from 'react-navigation';
import { ListSection, Divider } from 'react-native-paper';
import SimpleDrawer from './src/SimpleDrawer';
@@ -35,8 +35,8 @@ class Home extends React.Component {
}
}
const App = createStackNavigator({
Home,
const App = createSwitchNavigator({
Home: createStackNavigator({ Home }),
...data.reduce((acc, it) => {
acc[it.routeName] = {
screen: it.component,

View File

@@ -1,6 +1,6 @@
{
"expo": {
"name": "React Navigation Material Bottom Tabs Example",
"name": "React Navigation Drawer Example",
"description": "Demonstrates the various capabilities of react-navigation-drawer",
"slug": "react-navigation-drawer-demo",
"sdkVersion": "27.0.0",

View File

@@ -16,6 +16,10 @@ const MyNavScreen = ({ navigation, banner }) => (
title="Open other screen"
/>
<Button onPress={() => navigation.goBack(null)} title="Go back" />
<Button
onPress={() => navigation.navigate('Home')}
title="Go back to list"
/>
</SafeAreaView>
<StatusBar barStyle="default" />
</ScrollView>

View File

@@ -22,13 +22,17 @@ export default class DrawerView extends React.PureComponent {
}
componentDidUpdate(prevProps) {
const { isDrawerOpen } = this.props.navigation.state;
const { isDrawerOpen, key } = this.props.navigation.state;
const prevKey = prevProps.navigation.state.key;
const wasDrawerOpen = prevProps.navigation.state.isDrawerOpen;
const shouldOpen = this._shouldOpen(isDrawerOpen, wasDrawerOpen);
const shouldClose =
this._shouldClose(isDrawerOpen, wasDrawerOpen) || key !== prevKey;
if (this._shouldOpen(isDrawerOpen, wasDrawerOpen)) {
if (shouldOpen) {
this._drawerState = 'opening';
this._drawer.openDrawer();
} else if (this._shouldClose(isDrawerOpen, wasDrawerOpen)) {
} else if (shouldClose) {
this._drawerState = 'closing';
this._drawer.closeDrawer();
}