首页 > 解决方案 > 无法解决在 reactjs 中显示为未定义状态的错误?

问题描述

我是 reactjs 的新手,我的目标是制定保护路线并通过链接https://codesandbox.io/s/github/browniefed/tmp/tree/reactRouterProtectedRoute/但在我的本地代码中我收到错误像状态没有定义。我无法弄清楚我做错了什么。

export const PrivateRoute = ({ component: Component, loggedIn, path, ...rest }) => (
  <Route
    path={path}
    {...rest} render={props => {

      // authorised so return component
      return loggedIn ? (<Component {...props} />) : (<Redirect to={{
        pathname: "/Signin",
        state: {
          prevLocation: path,
          error: "You need to login first!",
        },
      }}

      />)
    }} />
)

我收到如下快照所示的错误:

折断

谁能帮我解决这个错误?

标签: reactjs

解决方案


你可以像这样包装你的组件 => withRouter(yourComponent);


推荐阅读