I am use react-native 0.70.6, and use Drawer navigation @react-navigation/drawer: "^6.5.6"
for the navigate.
When I press device back button i want to redirect previous menu(screen) but right now it’s close the app.
When I use navigation.goBack()
in my about.js
file it’s work properly but when i press device back button it’s close the app.
Here is my code
In my App.js
file
import MyDrawer from './sidemenubar';
<View style={{ flex: 1 }}>
<NavigationContainer>
<Stack.Navigator
initialRouteName="Splash"
screenOptions={{
headerShown: false,
orientation: 'portrait'
}}
>
<Stack.Screen name="Sidemenubar" component={MyDrawer} /> //for custom drawer
<Stack.Screen name="Splash" component={Splash} />
</Stack.Navigator>
</NavigationContainer>
<AlertDialog />
</View>
In my sidemenubar.js
file
import { createDrawerNavigator } from '@react-navigation/drawer';
const Drawer = createDrawerNavigator();
<Drawer.Navigator
drawerContent={props => <CustomDrawer {...props} />}
useLegacyImplementation={true}
initialRouteName="Dashboard">
<Drawer.Screen
name="Dashboard"
component={Dashboard}
/>
<Drawer.Screen
name="About"
component={About}
/>
</Drawer.Navigator>
How can i fix this issue Please help me…
Thanks.
2
Answers
You have to use the BackHandler from react-native.
Just register the
hardwareBackPress
event on mounting the component and don’t forget to remove it while onmounting:That should hopefully solve your issue.
For functional component.
You can try using useBackHandler hook.
Let me know does this helps you or not.