[英]react-router v4's Route doesn't pass it's props to component - in React Typescript
我刚刚开始使用React Router v4,无法使用Browserrouter的历史记录。 例如,当我尝试访问this.props.history.push("/")
我收到了错误:
TS2339: Property 'history' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'
我正在使用带有Browserrouter的React Router v4和带有Typescript的React。 似乎来自React Router的Route的三个道具(位置,历史,匹配)不会传递给它的组件。 我是使用Typescript还是使用React Router做错了什么?
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import './index.css';
import {
BrowserRouter as Router, Route
} from 'react-router-dom';
import Login from "./Login";
import Questionnaire from "./Questionnaire";
import Registration from "./Registration";
import NotFound from "./NotFound";
import GotNoHistory from "./GotNoHistory";
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(
<Router>
<Route exact={true} path="/" component={Questionnaire}/>
<Route path="/login" component={Login}/>
<Route path="/registration" component={Registration}/>
<Route path="/gotNoHistory" component={GotNoHistory} />
<Route component={NotFound}/>
</Router>,
document.getElementById('reactApp')
);
registerServiceWorker();
示例组件GotNoHistory.tsx
import * as React from 'react';
import './index.css';
class GotNoHistory extends React.Component {
render() {
return <div>{this.props.history.push("/")}</div>;
}
}
export default GotNoHistory;
更新
我发现了RouteComponentProps 。 我将这个道具添加到我的类GotNoHistory中 ,错误消失了。 这是解决问题的正确方法吗?
import {RouteComponentProps} from 'react-router';
class GotNoHistory extends React.Component<RouteComponentProps<any>, any> {
render() {
return <div>Go back {this.props.history.push("/")}</div>;
}
}
Chris解决方案的更新
import * as React from 'react';
import './index.css';
import {Route} from "react-router-dom";
interface Props {
}
interface State {
}
export default class GotNoHistory extends React.Component<Props & Route, State> {
render() {
return this.props.history.push("/");
}
}
导致几乎相同的错误: TS2339: Property 'history' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{Props & Route<RouteProps>}>'
TS2339: Property 'history' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{Props & Route<RouteProps>}>'
由我自己解决。
我的解决方案是将路径道具添加到我的道具类型中。
import {RouteComponentProps} from 'react-router';
class GotNoHistory extends React.Component<RouteComponentProps<any>> {
render() {
return <div>Go back {this.props.history.push("/")}</div>;
}
}
对于那些不知道的人,像我之前一样,这意味着什么<RouteComponentProps<any>>
:
参数是prop类型。 所以在它之前只有React.Component
,这意味着我们不希望我的路由器有任何道具。 所以这无法解决!
您应该在路径组件中访问它,但是您需要让Typescript知道您希望它们作为道具:
import { Route, Redirect } from 'react-router-dom'
import React from 'react'
interface IProps {
}
interface IState {
}
export default class GotNoHistory extends React.Component<IProps & Route, IState> {
render() {
this.props.history.push("/");
}
}
IProps & Route
这条线说的是预期的道具是我自己的道具(IProps,无论你定义为什么,可能没什么)和道具道具的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.