Cha*_*lie 15 typescript reactjs react-router typescript2.0 react-router-v4
我试图使用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 *** -->
) : (
<Redirect to={{
pathname: '/',
state: { from: props.location }
}} />
)
)} />
}
Run Code Online (Sandbox Code Playgroud)
这<React.Component {...thisProps} />是不对的.错误是:NodeInvocationException:inst.render不是函数TypeError:inst.render不是函数
Rob*_*bin 31
错误可能与输入中的输入和隐式返回有关.解决这个问题后,你最终会得到这样的结果:
const PrivateRoute = ({component, isAuthenticated, ...rest}: any) => {
const routeComponent = (props: any) => (
isAuthenticated
? React.createElement(component, props)
: <Redirect to={{pathname: '/login'}}/>
);
return <Route {...rest} render={routeComponent}/>;
};
Run Code Online (Sandbox Code Playgroud)
这个组件可以像这样使用:
<PrivateRoute
path='/private'
isAuthenticated={this.props.state.session.isAuthenticated}
component={PrivateContainer}
/>
Run Code Online (Sandbox Code Playgroud)
上面的解决方案有一些缺点.其中一个原因是您失去了类型安全性.
可能扩展Route组件是更好的主意.
import * as React from 'react';
import {Redirect, Route, RouteProps} from 'react-router';
export interface ProtectedRouteProps extends RouteProps {
isAuthenticated: boolean;
authenticationPath: string;
}
export class ProtectedRoute extends Route<ProtectedRouteProps> {
public render() {
let redirectPath: string = '';
if (!this.props.isAuthenticated) {
redirectPath = this.props.authenticationPath;
}
if (redirectPath) {
const renderComponent = () => (<Redirect to={{pathname: redirectPath}}/>);
return <Route {...this.props} component={renderComponent} render={undefined}/>;
} else {
return <Route {...this.props}/>;
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以你可以使用这样的组件:
const defaultProtectedRouteProps: ProtectedRouteProps = {
isAuthenticated: this.props.state.session.isAuthenticated,
authenticationPath: '/login',
};
<ProtectedRoute
{...defaultProtectedRouteProps}
exact={true}
path='/'
component={ProtectedContainer}
/>
Run Code Online (Sandbox Code Playgroud)
Sha*_*oon 10
对于react-router-dom (v6.0.2),您可以对PrivateRoute 组件使用以下代码:
import { FC } from 'react';
import { useAppSelector } from 'app/hooks';
import { Navigate } from 'react-router-dom';
interface PropType {
component: React.FC;
}
const PrivateRoute: FC<PropType> = ({ component: Component }) => {
const { isAuthenticated } = useAppSelector(state => state.auth);
if (isAuthenticated) return <Component />;
return <Navigate to='/login' />;
};
export default PrivateRoute;
Run Code Online (Sandbox Code Playgroud)
要在App.tsx中使用,您可以按如下方式使用它:
<Routes>
<Route path='/' element={<LandingPage />} />
<Route path='/login' element={<LoginPage />} />
<Route path='/home' element={<PrivateRoute component={HomePage} />} />
<Route path='*' element={<NotFound />} />
</Routes>
Run Code Online (Sandbox Code Playgroud)
小智 7
我的私人路线
import React from 'react'
import {Redirect, Route, RouteProps} from 'react-router'
export interface IPrivateRouteProps extends RouteProps {
isAuth: boolean // is authenticate route
redirectPath: string // redirect path if don't authenticate route
}
const PrivateRoute: React.FC<IPrivateRouteProps> = (props) => {
return props.isAuth ? (
<Route {...props} component={props.component} render={undefined} />
) : (
<Redirect to={{pathname: props.redirectPath}} />
)
}
export default PrivateRoute
Run Code Online (Sandbox Code Playgroud)
使用
<PrivateRoute isAuth={false} redirectPath="/login" path="/t1">
<Pages.Profile /> your`s protected page
</PrivateRoute>
Run Code Online (Sandbox Code Playgroud)
您仍然可以使用SFC表单,我发现它更干净。只需将所需的任何道具与混合即可RouteProps:
const PrivateRoute: React.SFC<RouteProps> = ({
component: Component,
...rest
}: {
component: React.ComponentType<RouteProps>;
}) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated
? <Component {...props} />
: <Redirect to="/login" />
}
/>
);
Run Code Online (Sandbox Code Playgroud)
小智 5
这真的帮助了我
import * as React from "react";
import { Route } from "react-router-dom";
interface IProps {
exact?: boolean;
path: string;
component: React.ComponentType<any>;
}
const LoggedOutRoute = ({
component: Component,
...otherProps
}: IProps) => (
<>
<header>Logged Out Header</header>
<Route
render={otherProps => (
<>
<Component {...otherProps} />
</>
)}
/>
<footer>Logged Out Footer</footer>
</>
);
export default LoggedOutRoute;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5984 次 |
| 最近记录: |