简体   繁体   English

在 React 中创建私有路由时遇到三元运算符的问题

[英]Having trouble with the ternary operator while creating private route in React

I'm following an online course and creating a private route component that redirects users based on an isAuthenticated state, I logged the state and it returned the else part even though the state was true.我正在学习在线课程并创建一个私有路由组件,该组件根据 isAuthenticated 状态重定向用户,我记录了状态并返回了 else 部分,即使状态为真。

  <Route
    {...rest}
    render={props =>
      isAuthenticated === true ? (
        <Redirect to="/login2" />
      ) : (
        <Redirect to="/login3" />
      )
    }
  />

it redirects to login3 even if the state is true.即使状态为真,它也会重定向到 login3。

From where are you getting isAuthenticated ?你从哪里得到 isAuthenticated ? Is it a props or are you using redux?它是道具还是您使用的是 redux?

If it props then write props.isAuthenticated it will work如果它是道具然后写props.isAuthenticated它会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM