Reactjs 在React中如何在专用路由中重定向?

Reactjs 在React中如何在专用路由中重定向?,reactjs,redirect,react-router,react-router-dom,Reactjs,Redirect,React Router,React Router Dom,我的PrivateRoute组件有问题。它应该在特定的情况下将用户重定向到特定的页面,但事实并非如此。登录后,我的条件有效,但没有重定向到正确的页面。我只看到/#,这是我使用的b2c的默认重定向 组件: const PrivateRoute: React.FC<PrivateRouteProps> = (props: PrivateRouteProps) => { const { authStatus } = props; ... if (authStatus ==

我的PrivateRoute组件有问题。它应该在特定的情况下将用户重定向到特定的页面,但事实并非如此。登录后,我的条件有效,但没有重定向到正确的页面。我只看到
/#
,这是我使用的b2c的默认重定向

组件:

const PrivateRoute: React.FC<PrivateRouteProps> = (props: PrivateRouteProps) => {
  const { authStatus } = props;
  ...
  if (authStatus === AuthStatus.SomeRole) {
    // conditional is working
    return <Route exact path="/specificUrl" render={() => <Redirect to="/specificUrl" />} />;
  }
  ...
};
const App: React.FC = () => (
  <Suspense fallback={<Loader />}>
    <Switch>
      ...
      <PrivateRoute path="/specificUrl" component={SpecificComponent} />
      ...
    </Switch>
  </Suspense>
);

您的第一个逻辑是有效的,但要重定向,您可以尝试将{withRouter}从react router dom中带出,然后在导出时用它包装您的组件,就像导出高阶组件一样。这将允许您访问历史记录道具,然后您可以使用history.push(“/specificUrl”)使用历史记录重定向

从“react router dom”导入{withRouter}
const PrivateRoute:React.FC=(props:PrivateRouteProps)=>{
const{authStatus,history}=props;
...
if(authStatus==authStatus.SomeRole){
//条件反射在起作用
返回history.push('/specificUrl')/>}/>;
//推送(“/specificUrl”)也可以工作
}
...
};
使用路由器导出默认值(PrivateRoute)

if (authenticationStatus === AuthenticationStatus.SignedInNotInPilot) {
  return <Redirect to="/specificUrl" />;
  // return <Route exact path="/specificUrl" render={() => <Redirect to="/specificUrl" />} />;
}
if (authenticationStatus === AuthenticationStatus.SignedInNotInPilot) {
  return <Route exact path="/specificUrl" render={() => <SpecificComponent />} />;
  // return <Redirect to="/specificUrl" />;
  // return <Route exact path="/specificUrl" render={() => <Redirect to="/specificUrl" />} />;
}