反应导航检查上一个屏幕是否存在 [英] React Navigation check if previous screen exists

查看:42
本文介绍了反应导航检查上一个屏幕是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命地寻找一种可能性,以检查ReactNavigation中是否存在先前的屏幕.

I am looking desperately for a possibility to check if a previous screen exists in ReactNavigation.

如果没有先前的路由,则使用 this.props.navigation.goBack()返回 false ,但是我无法使用它,因为如果存在先前的路由,我将被重定向.

Using this.props.navigation.goBack() returns false if no previous route exists, but I can not use it because if a previous route exists I get redirected.

是否可以检查我是否打开了该应用程序,而不是从另一个屏幕导航到主屏幕?

Is there a possibility to check if I opened the app instead of navigated from another screen to the Home screen?

谢谢.我没有使用Redux.这样会使这些东西变得更容易,但是我现在想避免使用它.

Thank you. I am not using Redux. It would make such stuff easier but I would like to avoid using it at the moment.

推荐答案

什么是解决方案(不确定是否是最好的解决方案),那就是在上一个屏幕中的param对象中花费.这样,如果存在参数,则意味着存在上一个屏幕.

What could be a solution (not sure that it's the best one) would be to spend in the param object the previous screen. With that, if the params exists would mean that a previous screen exists.

例如:

const navigateAction = NavigationActions.navigate({
  routeName: 'Profile',

  params: { previous_screen: 'CURRENT_SCREEN' },

  action: NavigationActions.navigate({ routeName: 'NEXT_SCREEN' }),
});

this.props.navigation.dispatch(navigateAction);

然后在下一个屏幕中:

const { navigation } = this.props;
if (navigation.state.params && navigation.state.params.previous_screen) {
  // A previous screen exists
} else {
  // No previous screen
}

这篇关于反应导航检查上一个屏幕是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆