Resetting the navigation stack for the home screen (React Navigation and React Native)

前端 未结 13 2330
终归单人心
终归单人心 2020-11-29 23:49

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

相关标签:
13条回答
  • 2020-11-30 00:02

    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); 
    
    0 讨论(0)
  • 2020-11-30 00:03

    React Navigation 5.x

    import { CommonActions } from '@react-navigation/native';
    
    navigation.dispatch(
      CommonActions.reset({
        index: 1,
        routes: [
          { name: 'Home' },
          {
            name: 'Profile',
            params: { user: 'jane' },
          },
        ],
      })
    );
    

    Available in Snack

    0 讨论(0)
  • 2020-11-30 00:07

    In React Navigation Versions 5.x

    You can use StackActions.replace in this version

    import { StackActions } from '@react-navigation/native';
    
    
    navigation.dispatch(
        StackActions.replace('Home', { test: 'Test Params' })
    )
    

    Full Example: (Available in Snack)

    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>
      );
    }
    
    
    0 讨论(0)
  • 2020-11-30 00:08

    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

    0 讨论(0)
  • 2020-11-30 00:10

    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' }]
    })
    
    0 讨论(0)
  • 2020-11-30 00:13

    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'
          });
    
    0 讨论(0)
提交回复
热议问题