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

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

  );

}


万千封印
浏览 63回答 2
2回答

GCT1015

您的代码有错误初始路由名称={名字?"Listings": "Profile"} 如果仔细观察,名字和问号运算符之间没有空格。代码应该是这样的初始路由名称={名字?“列表”:“个人资料”}

浮云间

不幸的是我没有对我的项目进行硬刷新。嗯。效果很好。谢谢您的帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript