I\'ve got a problem with the navigation of React Navigation and React Native. It is about resetting navigation and returning to the home screen.
I\'ve build a StackN
This is How I do it :
reset(){
return this.props
.navigation
.dispatch(NavigationActions.reset(
{
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Menu'})
]
}));
}
at least replace 'Menu' with 'Home'. You may also want to adapt this.props.navigation to your implementation.
In version > 2 follow this:
import { NavigationActions, StackActions } from 'react-navigation';
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'MainActivity' })],
});
this.props.navigation.dispatch(resetAction);
import { CommonActions } from '@react-navigation/native';
navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [
{ name: 'Home' },
{
name: 'Profile',
params: { user: 'jane' },
},
],
})
);
Available in Snack
You can use StackActions.replace
in this version
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.replace('Home', { test: 'Test Params' })
)
import * as React from 'react';
import { View, Button, Text } from 'react-native';
import { NavigationContainer, StackActions } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
function SplashScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text style={{fontSize:25,marginBottom:25}} >SPLASH SCREEN!</Text>
<Button
title="Replace (RESET) with Home"
onPress={() =>
navigation.dispatch(
StackActions.replace('Home', { test: 'Test Params' })
)
}
/>
<View style={{margin:10}}/>
<Button
title="Push Home on the stack"
onPress={() =>
navigation.dispatch(StackActions.push('Home', { test: 'Test Params' }))
}
/>
</View>
);
}
function HomeScreen({ navigation, route }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text style={{fontSize:25,marginBottom:25}}>Home Screen!</Text>
<Text style={{margin:10}}>{route.params.test}</Text>
<Button
title="Push same screen on the stack"
onPress={() => navigation.dispatch(StackActions.pop(1))}
/>
<View style={{margin:10}}/>
<Button
title="Pop one screen from stack"
onPress={() =>
navigation.dispatch(StackActions.push('Home', { test: 'Test Params' }))
}
/>
<View style={{margin:10}}/>
<Button
title="Pop to top"
onPress={() => navigation.dispatch(StackActions.popToTop())}
/>
</View>
);
}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Splash" component={SplashScreen} />
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
This works fine as of now:
import { NavigationActions, StackActions } from 'react-navigation'
resetStack = () => {
const navigateAction = NavigationActions.navigate({
routeName: 'Home',
params: {},
action: NavigationActions.navigate({ routeName: 'Home' }),
});
props.navigation.dispatch(navigateAction);
}
Found here in the docs: https://reactnavigation.org/docs/en/navigation-actions.html#reset
I found this way to go while using @react-navigation
Bashirpour's Answer. However, while trying out Functional components where you already have navigation in props
here is a neat way to write reset Stack action:
props.navigation.reset({
index: 0,
routes: [{ name: 'Dashboard' }]
})
NavigationActions.reset()
seems to be the preferable solution. One issue I ran into with it actions was the tab buttons. Tabs would still show even if I explicitly turned them off in component. If I used navigation.navigate() instead of doing this via reset()
it would work fine.
SomeComponentScreen.navigationOptions = {
header: null
};
A workaround for this annoyance that worked for me is to consecutively call multiple navigate
statements.
navigation.goBack(); // this would pop current item in stack
navigation.navigate({
routeName: 'SomeOtherComponent'
});