【发布时间】:2018-07-16 07:13:44
【问题描述】:
我需要使用 react-native-router-flux 在 componentWillMount 中获取先前的场景键,以检查我从哪个屏幕返回并根据条件进行一些操作。我看了很多,但没有找到答案。有没有办法做到这一点?
【问题讨论】:
我需要使用 react-native-router-flux 在 componentWillMount 中获取先前的场景键,以检查我从哪个屏幕返回并根据条件进行一些操作。我看了很多,但没有找到答案。有没有办法做到这一点?
【问题讨论】:
你可以通过
Actions.prevScene,
这将返回堆栈中上一个场景的键。
【讨论】:
很难说,但更容易的是在导航时传递变量,就像那里一样:
Actions.home({from: 'about'})
现在,Home 属性包含 from 变量,你可以处理这个。
【讨论】:
@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'
}
【讨论】: