如何获取以前的场景名称反应原生路由器通量

问题描述 投票:2回答:3

我需要使用react-native-router-flux来获取componentWillMount中的前一个场景键,以检查我从哪个屏幕返回并在条件下执行某些操作。我看了很多,但没有找到答案。有没有办法做到这一点?

react-native-router-flux
3个回答
0
投票

很难说,但导航时传递变量更容易,就像那样:

Actions.home({from: 'about'})

现在,Home道具包含from变量,你可以处理这个。


4
投票

您可以通过Actions.prevScene实现此目的,它将返回堆栈中前一个场景的键。


0
投票

@ sftgf的解决方案应该可行,但在react-native-router-flux 4.0.0-beta.28上,它会错误地为我返回当前场景。

这个Gist工作,并有额外的好处,让其他场景在堆栈上:

import {Actions} from react-native-reouter-flux

export function getPreviousScene (jumpCount = 1) { // if nothing is passed, it defaults to 1
  const index = Actions.prevState.routes.length - (jumpCount + 1) // because zero-based index

  if (index >= 0) {
    return Actions.prevState.routes[index].routeName
  }

  return 'home'
}
© www.soinside.com 2019 - 2024. All rights reserved.