【发布时间】:2020-11-06 12:22:39
【问题描述】:
我有一个组件可以自己呈现路线。我想使用 RouteComponentProps 访问 location 属性,但是当我将组件更改为以下方式时出现一些类型错误:
import { render } from 'react-dom';
import { Redirect, Route, Switch } from 'react-router';
import { BrowserRouter } from 'react-router-dom';
import React from 'react';
import RouteParams from './Routes';
import { map } from 'lodash';
const App = () => {
<BrowserRouter>
<Switch>
{map(RouteParams().routes, route => {
return (
<Route exact path={route.pathName} component={route.component} />
);
})}
<Redirect to="/a" />
</Switch>
</BrowserRouter>
}
render(<App />, document.getElementById('root'));
Routes.tsx
import React, { FunctionComponent } from "react";
import { RouteComponentProps, withRouter } from 'react-router';
const A = () => <p>A</p>
const B = () => <p>B</p>
const C = () => <p>C</p>
interface IRouteParams {
pathName: string;
search: string[];
label: string;
component:
| React.ComponentType<RouteComponentProps<any>>
| React.ComponentType<any>;
}
const RouteParams: FunctionComponent<RouteComponentProps> = (
{location}
) => {
const showForAdmin = true;
const {search} = location;
console.log(search);
const scenarioManagerRoute: IRouteParams = {
pathName: '/a',
search: ['a'],
label: 'a',
component: A,
};
const routes: IRouteParams[] = [
scenarioManagerRoute,
{
pathName: '/b',
search: ['b'],
label: 'B',
component: B,
},
];
if (showForAdmin) {
routes.push({
pathName: '/c',
search: ['c'],
label: 'C',
component: C,
});
}
return { routes, scenarioManagerRoute };
};
export default withRouter(RouteParams);
我在组件中以及在导入它时遇到错误。请指教。任何帮助表示赞赏。
错误是:
【问题讨论】:
-
有什么错误?
-
@NicholasTower 更新 OP
标签: reactjs typescript react-router react-router-dom react-router-v4