相关疑难解决方法(0)

如何使用TypeScript和React-Router 4重写受保护的路由器?

我试图使用TypeScript <PrivateRoute>在react-router 文档中创建一个as describe .谁能帮我吗?

react-router文件中的privateRoute:

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (
    fakeAuth.isAuthenticated ? (
      <Component {...props}/>
    ) : (
      <Redirect to={{pathname: '/login', state: { from: props.location }
   }}/>
  )
 )}/>
)
Run Code Online (Sandbox Code Playgroud)

下面是我的TypeScript版本(它不起作用):

const PrivateRoute = (theProps: { path: string, component: React.SFC<RouteComponentProps<any> | undefined> | React.ComponentClass<RouteComponentProps<any> | undefined> }) => {
    return <Route path={theProps.path} render={props => (
        fakeAuth.isAuthenticated ? (
            <React.Component {...theProps} /> <!-- **** It will raise error …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-router typescript2.0 react-router-v4

15
推荐指数
5
解决办法
5984
查看次数