React Router v4重定向传递状态未定义

Vla*_*tan 6 reactjs react-router react-redux

我有一个专用的Route / something,我希望只有登录后才能访问。我正在使用带有状态的重定向,但是在目标位置。状态未定义。

这是重定向:

<Redirect
      to={{
        pathname: "/login",
        state: { referrer: props.location }
      }}
    />
Run Code Online (Sandbox Code Playgroud)

在目标位置,我试图访问它,但是对于该州却未定义。

this.props.location.state is undefined and I can't get referrer
Run Code Online (Sandbox Code Playgroud)

我正在关注这个例子。可能是错误吗?

sul*_*lam 1

不,这不是一个错误。实际上,当您从页面 A重定向到页面 B 时,通过以下方式您肯定会得到this.props.location.state.

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

除此之外,(直接打开页面 A 或来自页面 B 以外的页面)您将获得 state undefined。因为在这种情况下,您不会将 props 传递state给页面 A。