React Native 在屏幕组件外获取导航对象
React Native get navigation object outside screen component
我需要能够从不一定是屏幕组件的模块导航和重置导航堆栈。这意味着,我不能使用:
const {navigate} = this.props.navigation;
就我而言,我需要在用户点击推送通知时正确地将用户重定向到正确的屏幕。我只有回调:
onNotification: function(notification) {
// show correct screen and reset navigation stack
}
我只是需要更灵活的导航方式。那么我该怎么做呢?
这是我的解决方案。我为所有屏幕实现了 base class。有了这个,我总是知道我在哪个屏幕,并且总是可以在需要时获取导航对象以重定向用户:
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;
}
}
在其他一些 component/module 中,我可以调用它来导航到不同的屏幕:
Base.screen.nav().navigate(...);
我创建了一个导航感知屏幕组件来在屏幕上处理它。
屏幕外可以直接访问store.navigation。在我的示例中,我将它与 redux 一起使用。看看对你有没有帮助。
我需要能够从不一定是屏幕组件的模块导航和重置导航堆栈。这意味着,我不能使用:
const {navigate} = this.props.navigation;
就我而言,我需要在用户点击推送通知时正确地将用户重定向到正确的屏幕。我只有回调:
onNotification: function(notification) {
// show correct screen and reset navigation stack
}
我只是需要更灵活的导航方式。那么我该怎么做呢?
这是我的解决方案。我为所有屏幕实现了 base class。有了这个,我总是知道我在哪个屏幕,并且总是可以在需要时获取导航对象以重定向用户:
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;
}
}
在其他一些 component/module 中,我可以调用它来导航到不同的屏幕:
Base.screen.nav().navigate(...);
我创建了一个导航感知屏幕组件来在屏幕上处理它。
屏幕外可以直接访问store.navigation。在我的示例中,我将它与 redux 一起使用。看看对你有没有帮助。