0
votes

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.

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

it redirects to login3 even if the state is true.

1
It it true immediately or is there some asynchronous stuff happening?Brian Thompson
if have "isAuthenticated" state available from props, change your code as below <Route {...rest} render={props => props.isAuthenticated === true ? ( <Redirect to="/login2" /> ) : ( <Redirect to="/login3" /> ) } />user2063635
They only refer to isAuthenticated as being state, not props. If that indeed is the issue, please read up on the difference here.Brian Thompson
@BrianThompson it's true before loading the pageHama Saadwn
Can you show where you are logging it?Brian Thompson

1 Answers

0
votes

From where are you getting isAuthenticated ? Is it a props or are you using redux?

If it props then write props.isAuthenticated it will work