Hri*_*mov 7 reactjs react-native react-navigation
我有以下路线结构:
StackNavigator
-StackNavigator
-TabNavigator
--Tab1
---Route 1 (Stack) (initial)
---Route 2 (Stack)
--Tab2
---Route 3 (Stack) (initial)
---Route 4 (Stack)
Run Code Online (Sandbox Code Playgroud)
当我访问Tab1 -> Route 1 -> Route 2 -> Tab2并返回时Tab1,活动路径是2而不是initialRoute1.
我正在做以下事情:
tabBarOnPress: ({ scene }) => {
const { route } = scene;
const tabRoute = route.routeName;
const { routeName } = route.routes[0];
navigation.dispatch(NavigationActions.navigate({ routeName: tabRoute }));
navigation.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName }),
],
}));
},
Run Code Online (Sandbox Code Playgroud)
但问题是它首先显示Route 2然后导航到Route 1.
如何重置之前的选项卡/屏幕,因此当我切换选项卡时,总是直接显示初始路径.
Hri*_*mov 13
5.xx 版本的解决方案:
将侦听器传递给屏幕组件:
<Tab.Screen
name="homeTab"
component={HomeStackScreen}
listeners={tabBarListeners}
/>
Run Code Online (Sandbox Code Playgroud)
然后在此侦听器上,每次用户按下选项卡时都对其进行导航:
const tabBarListeners = ({ navigation, route }) => ({
tabPress: () => navigation.navigate(route.name),
});
Run Code Online (Sandbox Code Playgroud)
学分:https : //github.com/react-navigation/react-navigation/issues/8583
版本 4.xx 的解决方案:
tabBarOnPress: ({ navigation }) => {
navigation.popToTop();
navigation.navigate(navigation.state.routeName);
}
Run Code Online (Sandbox Code Playgroud)
学分:https : //github.com/react-navigation/react-navigation/issues/1557
2.xx 和 3.xx 版本的解决方案:
问题是,当我重置路线时,我需要传递上一个 routeName(离开选项卡)的导航动作,并为下一条路线调度一个新的导航动作:
tabBarOnPress: ({ previousScene, scene }) => {
const tabRoute = scene.route.routeName;
const prevRouteName = previousScene.routes[0].routeName;
navigation.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: prevRouteName
}),
],
}));
navigation.dispatch(NavigationActions.navigate({
routeName: tabRoute
}));
}
Run Code Online (Sandbox Code Playgroud)
小智 7
使用 unmountOnBlur 选项,它适用于所有类型的导航器,堆栈导航器,抽屉导航器,底部选项卡导航器
<Tab.Screen name="Home" component={DrawerNavigator} options={{unmountOnBlur:true}}/>
在导航器屏幕的 options 属性中传递 unmountOnBlur
| 归档时间: |
|
| 查看次数: |
2075 次 |
| 最近记录: |