【发布时间】:2021-06-22 04:22:07
【问题描述】:
我正在使用 react js 打字稿创建身份验证。当登录不成功或 isLogin = false 时,我想将 URL 重定向到路径 "/" ,该路径具有 Login 组件。当登录成功或isLogin = true 时,我想将 URL 重定向到具有仪表板组件的仪表板路径。到这里URL已经成功重定向,但是组件没有被调用。
function App() {
const { state } = useContext(AuthContext)
return (
<BrowserContext>
<Switch>
{!state.isLogin
? <Redirect to="/" />
: <Redirect to="/dashboard" />
}
<Route exact path="/" component={Login} />
<Route path="/dashboard" component={Dashboard} />
<Route path="/register" component={Register} />
</Switch>
</BrowserContext>
)
}
【问题讨论】:
标签: reactjs react-router react-router-dom