我有一条私有(private)路线/某物,我希望只有在登录后才能访问它。我正在对状态使用重定向,但在目标位置上。状态未定义。
这是重定向:
<Redirect
to={{
pathname: "/login",
state: { referrer: props.location }
}}
/>
在目的地,我尝试访问它,但状态未定义。
this.props.location.state is undefined and I can't get referrer
我正在关注this example 。可能是一个错误?
最佳答案
不,这不是一个错误。实际上当你从页面A重定向到页面B时,通过以下方式你肯定会得到this.props.location.state
。
<Redirect to={{pathname: "/b", state: { referrer: currentLocation }}} />
除此之外,(直接打开页面 A 或来自页面 B 以外的页面)您将获得状态 undefined
。因为在这种情况下,您没有传递 Prop state
到页面 A。
关于javascript - React Router v4 重定向传递状态未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54954556/