我正在开发一个由 react-native@0.55.4
和 react-native-navigation@1.1.474
提供支持的应用程序 .
最初,只有一个登录屏幕(使用 Navigation.startSingleScreenApp
) . 当用户登录时,我调用 Navigation.startTabBasedApp
(其中一个标签组件是我的 NavScreen
) . 每当用户更改为另一个选项卡时,应该显示选项卡堆栈的根,所以我尝试了这样的事情:
class NavScreen extends React.PureComponent {
constructor(props) {
super(props)
this.props.navigator.setOnNavigatorEvent(this.toRootOnTabSelect.bind(this))
}
toRootOnTabSelect(event) {
const {id} = event
if (["bottomTabSelected", "bottomTabReselected"].includes(id)) {
this.props.navigator.popToRoot({
animated: true,
animationType: "fade",
})
}
}
render() {
return <Text>Whatever...</Text>
}
}
但由于某些原因,我更改标签时没有调用 toRootOnTabSelect
事件处理程序方法(通过单击它们 - 而不是通过调用 switchToTab
API方法) .
我在网上发现了多个帖子(即https://stackoverflow.com/a/51159091/6928824,https://github.com/wix/react-native-navigation/issues/648),表明它应该有效,所以我不会错过任何帖子 . :/
任何帮助是极大的赞赏! :)
2 回答
可能导致这种情况的原因之一是
setOnNavigatorEvent
与addOnNavigatorEvent
结合使用,如果您有一个实现addOnNavigatorEvent
的屏幕包装器组件,那么您当前的侦听器将无效 .如documentation中所述
也
我建议尝试
addOnNavigatorEvent
而不是setOnNavigatorEvent
这似乎是
react-native-navigation@^1.1.474
中的一个错误(注意插入符号):见my issue on GitHub .according pull request已经打开但尚未合并 . :(