【问题标题】:react navigation v5 How to reset or replace?react navigation v5 如何重置或更换?
【发布时间】:2020-12-08 23:59:21
【问题描述】:

我尝试了文档中的所有示例,但没有成功。

我有一个要从这里路由到组件的组件。

我需要卸载组件 A。

我试过更换和重置。

            RootNavigation.navigationRef.current.dispatch(state => {
                return CommonActions.reset({
                    index: 0,
                    key: null,
                    routes: [{ name: 'B' }],
                });
            });

此代码函数将我路由到堆栈['AA','B'] 中的不同路由路由,因此它将我路由到'AA'

 const resetAction = StackActions.replace('B', {});
                    RootNavigation.navigationRef.current.dispatch(state => {
                        return resetAction;
                    });

我收到一个错误,他使用有效负载 {"name":"B","params":{}} 的操作“替换”未由任何导航器处理。

RootNavigation.navigate('B');

这个功能很好用,但它没有卸载 A。

【问题讨论】:

  • 有什么解决办法吗?

标签: react-native react-navigation react-navigation-v5


【解决方案1】:

阅读更多:https://reactnavigation.org/docs/stack-actions/#replace

import { StackActions } from '@react-navigation/native';

navigation.dispatch(
  StackActions.replace('Profile', {
    user: 'jane',
  })
);

【讨论】:

    【解决方案2】:

    you can use like this, create file name 'RootNavigation' and then enter these lines

      import {createNavigationContainerRef} from '@react-navigation/native';
      import {StackActions} from '@react-navigation/native';
    
      export const navigationRef = createNavigationContainerRef();
    
    
      export function navigate(name, params) {
        if (navigationRef.isReady()) {
          navigationRef.navigate(name, params);
        }
      }
    
      export function navigateReplace(name, param) {
        if (navigationRef.isReady()) {
          navigationRef.dispatch(
            StackActions.replace(name, {
              param,
            }),
          );
        }
      }
    
    

    if you wont to navigate without navigation props. import 'RootNavigation', like this

    import * as RootNavigation from '../RootNavigation'; 
    

    then you can navigate like this

    RootNavigation.navigateReplace('Login Flow');
    

    or

    RootNavigation.navigate('Profile');
    

    please refer `https://reactnavigation.org/docs/navigating-without-navigation-prop/` this official document

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2023-03-16
      • 2020-06-07
      • 2021-12-23
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2021-02-09
      • 2020-06-13
      相关资源
      最近更新 更多