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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With