How can I keep a React Native Drawer sticky?

50 Views Asked by At

ere's the code for the component

<NavigationContainer>
      <Header deviceUid={deviceUid} />
      <Drawer.Navigator
        drawerContent={props => <MotionMenu {...props} />}
        screenOptions={{
          drawerPosition: 'right',
          drawerType: 'front',
          swipeEnabled: false,
          drawerStyle: {
            width: 320,
          },
          drawerContentContainerStyle: {
            alignItems: 'center',
            alignContent: 'center',
            height: '100%',
          },
          headerShown: false,
          detachInactiveScreens: false,
        }}>
        <Drawer.Screen
          screenOptions={{
            headerShown: false,
            unmountOnBlur: false,
          }}
          name="App"
          component={TabNavigation}
        />
      </Drawer.Navigator>
    </NavigationContainer>

I tried "drawerType: 'permanent'" but that just aligns the View & Drawer next to each other.

1

There are 1 best solutions below

0
Aqeel Ahmad On

Try this sample and modify your code and navigation configuration accordingly:

import * as React from 'react';
import { createDrawerNavigator, createStackNavigator } from '@react-navigation/drawer';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';

// Sample screens (replace with your actual screens)
function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
      <Button title="Go to Settings" onPress={() => navigation.navigate('Settings')} />
    </View>
  );
}

function SettingsScreen() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Settings Screen</Text>
    </View>
  );
}

function ProfileScreen() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Profile Screen</Text>
    </View>
  );
}

function NotificationsScreen() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Notifications Screen</Text>
    </View>
  );
}

// Create the tab navigator
const Tab = createBottomTabNavigator();

const TabNavigator = () => {
  return (
    <Tab.Navigator>
      <Tab.Screen name="Home" component={HomeScreen} />
      <Tab.Screen name="Notifications" component={NotificationsScreen} />
    </Tab.Navigator>
  );
};

// Create the stack navigator
const Stack = createStackNavigator();

const StackNavigator = () => {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Tab" component={TabNavigator} />
      <Stack.Screen name="Settings" component={SettingsScreen} />
    </Stack.Navigator>
  );
};

// Create the drawer navigator
const Drawer = createDrawerNavigator();

const DrawerNavigator = () => {
  return (
    <Drawer.Navigator>
      <Drawer.Screen name="Home" component={StackNavigator} />
      <Drawer.Screen name="Profile" component={ProfileScreen} />
    </Drawer.Navigator>
  );
};

export default function App() {
  return (
    <NavigationContainer>
      <DrawerNavigator />
    </NavigationContainer>
  );
}


Hope it will help you.