简体   繁体   English

如何添加类型以响应专用路由器组件?

[英]How add types for react private router component?

I wanna add private router for auth users. 我想为身份验证用户添加专用路由器。 And find simple solution with private router component, but typescript return me type error. 并找到带有专用路由器组件的简单解决方案,但打字稿返回我键入错误。 I tried to add types any, but that doesn't help. 我尝试添加任何类型,但这无济于事。 How I may add types here? 我如何在这里添加类型?

Error:(18, 18) TS2322: Type '{ exact: true; path: string; component: typeof Main; }' is not assignable to type 'IntrinsicAttributes & Component<any, any, any>'.
  Property 'exact' does not exist on type 'IntrinsicAttributes & Component<any, any, any>'.

Routers 路由器

import { Route, Switch } from 'react-router-dom';
import React, { Component } from 'react';
import app from 'firebase/app';

import Main from "src/Templates/Main/Main.container";
import Login from "src/Templates/Login/Container/Login";
import PrivateRoute from "src/Core/Routers/PrivateRoute";
import {withFirebase} from "src/Templates/Firebase";


class Routes extends Component<any, any> {


    render() {
        return (
            <Switch>
                <PrivateRoute exact  path='/' component={Main} />
                <Route exact path='/login' component={Login} />
            </Switch>
        )
    }
}

export default Routes;

PrivateRouter 专用路由器

import React, {Component} from 'react';
import { Redirect, Route } from 'react-router-dom';


const PrivateRoute = (component: Component<any, any>, { ...rest }: any): any => (
    <Route {...rest} render={(props: any) => (
    props.auth !== null ? (
        <Component {...props} />
    ) : (
    <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
        }}
    />
        )
    )} />
);

//withFirebase() its my HOC fabric component, with provides props for check auth user.

export default withFirebase(PrivateRoute);

When you use type as any, you are ruining the taste of typescript. 当您将type用作任何类型时,将破坏打字稿的味道。

Now, looking at your code, I think you want to take the user to a particular path on the basis of the value of auth in props. 现在,看一下您的代码,我认为您想根据prop中auth的值将用户带到特定路径。 and all the remaining props should be same as of react-router. 并且所有其他道具应与react-router相同。

And a clear error I can see in your code is you are receiving each prop as a parameter in the functional component, but it only receives a single parameter, that is the props object. 我可以在您的代码中看到一个明显的错误:您正在将每个道具作为功能组件中的一个参数来接收,但是它仅接收一个参数,即props对象。

So in your approach, it should be, 所以在您的方法中,应该是

const PrivateRoute = (props: any): any => ()

Now, a cleaner and better approach would be: 现在,一种更清洁,更好的方法是:

import React, {Component} from 'react';
import { RouteProps, Route, Redirect } from 'react-router-dom';
export interface PrivateRouteProps extends RouteProps {
   auth: boolean | null
}

function PrivateRoute({ component: Component, auth, ...rest }: PrivateRouteProps) {
    return (
        <Route
            {...rest}
            render={(props) => auth !== true
                ? <Component {...props} />
                : <Redirect to={{ pathname: '/login', state: { from: props.location } }} />}
        />
    )
}



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM