【问题标题】:React Native get navigation object outside screen componentReact Native 获取屏幕组件外的导航对象
【发布时间】:2017-07-22 16:32:47
【问题描述】:

我需要能够从不一定是屏幕组件的模块中导航和重置导航堆栈。这意味着,我不能使用:

const {navigate} = this.props.navigation;

在我的情况下,当用户点击推送通知时,我需要正确地将用户重定向到正确的屏幕。我只有一个回调:

onNotification: function(notification) {
    // show correct screen and reset navigation stack
}

我只是需要更多的导航灵活性。那么我该怎么做呢?

【问题讨论】:

    标签: react-native react-navigation react-native-navigation react-native-push


    【解决方案1】:

    这是我的解决方案。我为所有屏幕实现了基类。有了这个,我总是知道我在哪个屏幕,并且总是可以在需要时让导航对象重定向用户:

    import React, {Component} from 'react';
    
    export default class Base extends Component {
        static screen;
    
        constructor(props) {
            super(props);
            Base.screen = this; 
        }
    
        nav() {
            return this.props.navigation;
        }
    }
    

    在其他一些组件/模块中,我可以调用它来导航到不同的屏幕:

    Base.screen.nav().navigate(...);
    

    【讨论】:

      【解决方案2】:

      我创建了一个导航感知屏幕组件来处理屏幕上的问题。

      对于屏幕外,您可以直接访问 store.navigation。在我的示例中,我已将它与 redux 一起使用。看看这对你有没有帮助。

      https://github.com/aajiwani/react-navigation-aware-helper

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多