React Navigation V5 InitalRouteName 不适用于条件渲染

React Navigation V5 InitalRouteName not working with conditional rendering

如果用户的信息尚不存在,我想将用户带到个人资料页面。但是在 jwt 中使用条件渲染时,initialRouteName 不起作用。也许我错过了更好的方法。有人有什么建议吗?

function ExploreNavigator() {
  const jwt = useSelector(state => state.session.jwt);
  const { firstName } = useSelector(state => state.user);

  return (
    <Stack.Navigator
      headerMode="screen"
      initialRouteName={firstName ? "Listings" : "Profile"}
      screenOptions={{
        cardStyle: {
          backgroundColor: 'white',
        },
      }}>
      {jwt ? (
        <>
          <Stack.Screen
            name="Listings"
            component={ListingsScreen}
            options={{ title: 'My Studios' }}
          />

          <Stack.Screen
            name="Onboarding"
            component={OnboardingScreen}
            options={{
              headerShown: false,
            }}
          />
          <Stack.Screen
            name="Profile"
            component={ProfileScreen}
            options={{
              headerShown: false,
            }}
          />
        </>
      ) : (
        <>
          <Stack.Screen
            name="Sign Up"
            component={SignUpScreen}
            options={{
              headerShown: false,
            }}
          />
          <Stack.Screen
            name="Login"
            component={LoginScreen}
            options={{
              headerShown: false,
            }}
          />
        </>
      )}
    </Stack.Navigator>
  );
}

您的代码有误

initialRouteName={firstName? “清单”:“个人资料”} 如果仔细观察,firstName 和问号运算符之间没有 space。代码应该是这样的

initialRouteName={名字? “列表”:“个人资料”}

不幸的是,我没有对我的项目进行硬刷新。嗯。它工作正常。感谢您的帮助。