gpt4 book ai didi

javascript - 如何组织授权的react路线?

转载 作者:行者123 更新时间:2023-12-03 01:02:43 25 4
gpt4 key购买 nike

如果您重定向或显示登录页面,您如何正确组织索引路线?这看起来很乱:

<Switch>
<Route
path="/"
exact
render={() => (!isLogged ? <Landing /> : <Redirect to="/dashboard" />)}
/>

您还可以将其包装在 if-else 语句中。

if (!isLogged) {  
routes = (
<Route path="/" component={Landing} />
....
)
} else {
routes = (
<Route path="/" render={() => <Redirect to="/dashboard"} />
.....
)
}

但这也感觉很困惑......有什么解决方案吗?

最佳答案

一个选项是使用返回 <Route /> 的包装组件。如果用户已登录,则为登陆组件。

const LoggedInRoute = (props) => {
if(!isLoggedIn) {
return <Landing />
}
return <Route {...props} />
}

用法:

<LoggedInRoute 
path="/"
exact
render={() => <Redirect to="/dashboard" />}
/>

关于javascript - 如何组织授权的react路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52555320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com