0

I have the following in my react app:

<PrivateRoute 
  path="/profile" 
  component={ Profile }
  authenticated={ this.state.authenticated }

/>

<PrivateRoute/> is basically just the below:

const PrivateRoute = ({ component : Component , authenticated : auth , ...rest }) => {
  return (
    <Route {...rest} render={ (props) => {
      return (
        auth ? <Component {...props} /> 
        : <Redirect 
            to={{
                pathname : '/login',
                state : {
                    from : props.location
                }
            }} />
      )
    } } />
  )
}

As you can see in the above code there is a auth variable , how do i send this variable to now i would like to send this variable alog with the <Redirect /> which basically loads the <Login /> component , but how exactly do i send the auth variable alog with the Redirect component ?

1
  • Like you are sending location. Commented May 24, 2019 at 10:30

2 Answers 2

2

You can pass props data with Redirect like this:

const PrivateRoute = ({ component : Component , authenticated : auth , ...rest }) => {
return (
<Route {...rest} render={ (props) => {
  return (
    auth ? <Component {...props} /> 
    : <Redirect 
        to={{
            pathname : '/login',
            state : {
                from : {auth : auth}
            }
        }} />
  )
} } />

) }

and this is how you can access it:

this.props.location.state.auth
Sign up to request clarification or add additional context in comments.

1 Comment

I guess it should be state: { from: props.location, auth: auth }
0

Use this.props.history.push and access that variable by this.props.location

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.