【问题标题】:React Navigation V5 InitalRouteName not working with conditional renderingReact Navigation V5 InitalRouteName 不适用于条件渲染
【发布时间】:2020-12-17 15:03:42
【问题描述】:

如果用户的信息尚不存在,我想将其带到个人资料页面。但是在使用 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>
  );
}

【问题讨论】:

    标签: javascript react-native react-navigation react-navigation-v5


    【解决方案1】:

    你的代码有错误

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

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

    【讨论】:

    • 我只是输入了一个例子。如果设置 initalRouteName={"Profile"} 它仍然默认为“Listings”。
    • 通常,我所做的是,我没有在堆栈导航器内的 jwt 令牌上使用三元,而是将身份验证和主堆栈分离到它们自己的文件中,然后在主导航器文件中执行三元看起来像这样 {jwt ? : }
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2020-08-08
      • 2020-07-24
      • 2021-07-04
      • 1970-01-01
      • 2022-07-22
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多