【发布时间】:2021-02-07 10:17:30
【问题描述】:
假设标签导航器中有两个堆栈屏幕:
- 选项卡 A -> 相机
- 选项卡 B -> 个人资料
在配置文件屏幕中,其他相同类型的屏幕(“配置文件”)被推送到其堆栈中(具有不同的参数)。现在,如果您在“相机”屏幕中并执行以下操作:
navigation.navigate("Profile", { screen: "Profile", params });
您将导航到“配置文件”屏幕,这些参数将被发送到堆栈中的最后一个屏幕。如果我想导航到传递参数的堆栈根,我该怎么办?
我试过了:
// In the profile screen
useEffect(() => {
if (navigation.canGoBack())
navigation.popToTop(); // Go back to the root of the stack
showParams(params);
}, [params])
但是有了这个,“showParams”操作不会在根目录中执行,我也不会从“相机”屏幕直接导航到堆栈的根目录。
我认为在导航之前我必须在“相机”屏幕中执行以下操作:
navigation.dispatch(
CommonActions.reset({
// some stuff
})
);
navigation.navigate("Profile", { screen: "Profile", params });
但我找不到任何方法来实现我的目标。
有什么想法吗?谢谢。
更新 - 我的导航系统
STACKS(这里我定义了多个堆栈:“HomeStacks”、“SearchStacks”、“ProfileStacks”...)
const Stack = createStackNavigator();
export function ProfileStacks() { <------ Over this stack I do .push()
return (
<Stack.Navigator
initialRouteName="Profile"
>
<Stack.Screen name="Profile" children={Profile} />
<Stack.Screen name="EditProfile" children={EditProfile} />
</Stack.Navigator>
);
}
...
底部标签导航器
<Tab.Navigator>
<Tab.Screen
name="Camera"
component={CameraPlaceholder}
listeners={({ navigation }) => ({
tabPress: (event) => {
event.preventDefault();
navigation.navigate("CameraModal");
},
})}
/>
<Tab.Screen
name="Profile"
component={ProfileStacks}
/>
</Tab.Navigator>
ROOT STACK NAVIGATOR(应用的主导航器)
在这个堆栈中,我实现了身份验证流程,并且还声明了一些额外的堆栈(仅用于外观目的)。
export default function RootNavigator(props) {
/*
This navigator is implemented using the
'Protected Routes' pattern
*/
const { isUserLoggedIn } = props;
const RootStack = createStackNavigator();
return (
<RootStack.Navigator>
{isUserLoggedIn ? (
<>
<RootStack.Screen
name="BottomTabNavigator"
component={BottomTabNavigator}
/>
<RootStack.Screen
name="CameraModal"
component={Camera}
/>
</>
) : (
<>
<RootStack.Screen name="SignIn" component={SignIn} />
<RootStack.Screen
name="SignUp"
component={SignUp}
/>
<RootStack.Screen
name="ForgotPassword"
component={ForgotPassword}
/>
</>
)}
</RootStack.Navigator>
);
我见过的相关问题
How to reset a Stack in a different Tab using React Navigation 5.x
https://github.com/react-navigation/react-navigation/issues/6639
https://github.com/react-navigation/react-navigation/issues/8988
这是我的个人资料标签的导航数据
Object {
"key": "Profile-Ty4St1skrxoven-jkZUsx",
"name": "Profile",
"params": undefined,
"state": Object {
"index": 1,
"key": "stack-8nWDnwDJZRK8iDuJok7Hj",
"routeNames": Array [
"Profile",
"EditProfile",
],
"routes": Array [
Object {
"key": "Profile-m0GQkvNk5RjAhGABvOy9n",
"name": "Profile",
"params": undefined,
},
Object {
"key": "Profile-tZAEmSU0eEo1Nt7XC09t1",
"name": "Profile",
"params": Object {
"otherUserData": Object {
"username": "jeffbezos",
},
"post": null,
},
},
],
"stale": false,
"type": "stack",
},
},
],
"stale": false,
"type": "tab",
},
我只需要从我的应用程序的另一个选项卡的“配置文件”选项卡中的堆栈“配置文件”中弹出第二条路由,然后导航到此屏幕。
【问题讨论】:
-
你好胜利者,你可以放堆栈代码,我完全不明白你是否在那里嵌套了导航器以及你对堆栈根的含义。你也可以在这个有反应导航 v5 snack.expo.io/@anthowm/drawer-navigation-%7C-react-navigation 的小吃中展示你的案例
-
好的,我会在几分钟内更新它。
-
@anthonywillismuñoz 完成。问题是我做了 .push("Profile", { otherUserParams });在 ProfileStacks 中,我想从“相机”路线“弹出”所有这些推送的屏幕。仅仅因为如果我在不重置堆栈的情况下从“相机”导航到“配置文件”,我只会看到堆栈中最后推送的屏幕,而不是位于堆栈“根”的屏幕。
-
为什么你不直接推送到你想要的屏幕而不是让导航器选择要显示的屏幕。你知道你可以导航 navigation.navigate('Profile', { screen: 'Profile or EditProfile', params: {...} });。此外,如果这不是您想要的,您可以尝试通过不和谐 anthowm#7115 中的 dm 向我解释,我们可以用西班牙语聊天 :)
-
是的,我试过这个......我将通过不和谐更好地解释这个问题。谢谢伙计,我从昨天开始就在努力解决这个问题。
标签: javascript reactjs react-native react-navigation