【问题标题】:React Native Navigation: Navigate to a screen from outside a React componentReact Native Navigation:从 React 组件外部导航到屏幕
【发布时间】:2021-08-19 14:08:21
【问题描述】:

https://wix.github.io/react-native-navigation/docs/basic-navigation#navigating-in-a-stack 表示将新屏幕推送到堆栈需要当前屏幕的componentId。我的用例是允许基于本机模块发出的某些事件进行导航。因此,componentId 对我不可用,因为事件侦听器将驻留在任何 React 组件屏幕之外。有没有办法从RNN外部导航到屏幕?甚至获取当前的componentId

【问题讨论】:

    标签: react-native wix-react-native-navigation


    【解决方案1】:

    我最终添加了一个命令事件侦听器以将当前的componentId 存储在闭包中。

    let currentComponentId;
    
    Navigation.events().registerCommandListener((name, params) => {
      if (name === 'push') {
        currentComponentId = params.componentId;
      }
    });
    
    Navigation.events().registerAppLaunchedListener(() => {
      Navigation.setRoot(rootRouteConfig);
    
      EventEmitter.addListener('navigate', (name) => {
        Navigation.push(currentComponentId, {
          component: {
            name,
          },
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      相关资源
      最近更新 更多