Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router v4 Redirect Passing State Undefined

I have a private Route /something and I want it to be accessible only if logged in. I'm using Redirect with the state, but on destination location.state is undefined.

Here is the redirect:

<Redirect
  to={{
    pathname: "/login",
    state: { referrer: props.location }
  }}
/>

And in the destination, I' trying to access it, but I got undefined for the state.

this.props.location.state is undefined and I can't get referrer

I'm following this example. Could be a bug?

like image 390
Vlad Stan Avatar asked Mar 02 '19 02:03

Vlad Stan


1 Answers

No its not a bug. Actually when you redirect from page A to page B, by the following way you will definitely get the this.props.location.state.

<Redirect to={{pathname: "/b", state: { referrer: currentLocation }}} />

Other than this, (direct open page A or coming from other than page B) you will get state undefined. Because in that scenario you are not passing the props state to page A.

like image 60
Sultan Aslam Avatar answered Nov 17 '22 03:11

Sultan Aslam