[英]React Router props: what is the correct type?
I create a router custom component to do a redirect in case some parameters are not present:如果某些参数不存在,我会创建一个路由器自定义组件来执行重定向:
const StateParamRoute = (props: any) => {
...
return stateParam ? <Route {...props} /> : <Redirect to="/error" />;
};
Then, I use it as:然后,我将其用作:
<BrowserRouter>
<Switch>
<StateParamRoute path="/" exact component={TestScreen} />
</Switch>
</BrowserRouter>
But I am getting a warning in the console saying:但是我在控制台中收到警告说:
Line 48:23: Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
I know I can ignore this, or disable the checking on eslint config, but I would like to solve it.我知道我可以忽略这个,或者禁用对 eslint 配置的检查,但我想解决它。
I have tried:我努力了:
type CustomRouterProps = {
path: string,
exact: boolean,
component: React.ReactNode
}
const StateParamRoute = (props: CustomRouterProps) => {
...
}
But it does not work...但它不起作用......
So far I found if I do:到目前为止,我发现如果我这样做:
type CustomRouterProps = {
path: string;
exact: boolean;
component: ComponentType<ReactNode>;
};
const StateParamRoute = (props: CustomRouterProps) => {
...
}
The warning is gone, but I am not sure if it is the best solution.警告消失了,但我不确定它是否是最好的解决方案。
Why don't you just use the types from @types/react-router-dom
and import it like so:为什么不直接使用来自
@types/react-router-dom
的类型并像这样导入它:
import { RouteProps } from 'react-router-dom';
const StateParamRoute = (props: RouteProps) => {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.