[英]React PrivateRoute functional component in TypeScript
I get the following error:我收到以下错误:
No overload matches this call.
Overload 1 of 2, '(props: Readonly<RouteProps>): Route<RouteProps>', gave the following error.
Type 'Element' is not assignable to type '(props: RouteComponentProps<any, StaticContext, PoorMansUnknown>) => ReactNode'.
Type 'Element' provides no match for the signature '(props: RouteComponentProps<any, StaticContext, PoorMansUnknown>): ReactNode'.
Overload 2 of 2, '(props: RouteProps, context?: any): Route<RouteProps>', gave the following error.
Type 'Element' is not assignable to type '(props: RouteComponentProps<any, StaticContext, PoorMansUnknown>) => ReactNode'. TS2769
32 |
33 | if (authStatus === AuthStatus.SomeRole) {
> 34 | return <Route exact path="/someUrl/" render={<Redirect to="/someUrl" />} />;
| ^
35 | }
Interface:界面:
interface IPrivateRouteProps {
component: React.FC;
path: string;
exact?: boolean;
}
Type类型
type PrivateRouteProps = RouteProps &
IPrivateRouteProps &
IAuthenticationState &
typeof AuthenticationActionCreators.actionCreators;
Component:零件:
const PrivateRoute: React.FC<PrivateRouteProps> = (props: PrivateRouteProps) => {
const { authStatus } = props;
...
if (authStatus === AuthStatus.SomeRole) {
return <Route exact path="/someUrl/" render={<Redirect to="/someUrl" />} />;
}
...
};
Can you explain to me where this error comes from?你能解释一下这个错误是从哪里来的吗? If I remove the
render
and replace the component
in <Route component={<SomeComponent />} />
, everything works, only the component does not render itself.如果我删除
render
并替换 <Route component
<Route component={<SomeComponent />} />
中的组件,一切正常,只有组件不渲染自身。
If you go through react router documentation:如果你 go 通过反应路由器文档:
render: func
渲染:函数
render requires function. So in your case it should be like this:渲染需要 function。所以在你的情况下它应该是这样的:
if (authStatus === AuthStatus.SomeRole) {
return <Route exact path="/someUrl/" render={(routeProps) => <Redirect to="/someUrl" />} />;
}
If you go little more depth:如果你 go 更深入一点:
Warning : <Route component>
takes precedence over <Route render>
so don't use both in the same.警告:
<Route component>
优先于<Route render>
所以不要同时使用两者。
Hope you got the answer.希望你得到答案。 More you can read here
更多你可以在这里阅读
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.