【发布时间】:2021-04-30 23:00:05
【问题描述】:
如果我点击“/”或“/reset_password”或任何其他路由,PurelyUnauthenticatedRoutes 中的第一个组件总是会被调用,即带有消息 ROUTE_CONSTANTS.RESET_PASSWORD 的 PublicRoute
<Router>
<Switch>
<PurelyUnauthenticatedRoutes />
<PublicRoute key={1} redirect={false} path="/" component={CheckAuthenticatedRoutes} />
</Switch>
</Router>
const PurelyUnauthenticatedRoutes = (props) => {
return [
<PublicRoute
{...props}
key={"/reset_password"}
redirect={false}
path={"/reset_password"}
render={(routeProps) => <div>{ROUTE_CONSTANTS.RESET_PASSWORD}</div>}
/>,
];
};
const PublicRoute = (props) => {
const { userAuthenticationDetails, redirect } = props;
const isLoggedIn = (userAuthenticationDetails && userAuthenticationDetails.isLoggedIn) || false;
if (isLoggedIn && redirect) {
return <Redirect to={"/dashboard"} />;
}
return <Route {...props} />;
};
export default PublicRoute;
【问题讨论】:
-
你应该在路由上使用
exact属性,否则它只会匹配第一个匹配的:reactrouter.com/web/api/Route/exact-bool -
@Jayce444 我应该在哪里以及为什么要准确放置。理想情况下,如果我要说“/”,那么它应该转到 CheckAuthenticatedRoutes 组件,但它会转到 PurelyUnauthenticatedRoutes 第一个元素
标签: reactjs routes react-router router