我需要使用react-native-router-flux来获取componentWillMount中的前一个场景键,以检查我从哪个屏幕返回并在条件下执行某些操作。我看了很多,但没有找到答案。有没有办法做到这一点?
很难说,但导航时传递变量更容易,就像那样:
Actions.home({from: 'about'})
现在,Home
道具包含from
变量,你可以处理这个。
您可以通过Actions.prevScene
实现此目的,它将返回堆栈中前一个场景的键。
@ 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'
}