简体   繁体   English

react-router v4的Route不会将它的道具传递给组件 - 在React Typescript中

[英]react-router v4's Route doesn't pass it's props to component - in React Typescript

I just started to use React Router v4 and can't manage to use the history of Browserrouter. 我刚刚开始使用React Router v4,无法使用Browserrouter的历史记录。 For example, when I try to access this.props.history.push("/") I got the error: 例如,当我尝试访问this.props.history.push("/")我收到了错误:

TS2339: Property 'history' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'

I'm using React Router v4 with Browserrouter and React with Typescript. 我正在使用带有Browserrouter的React Router v4和带有Typescript的React。 It seems that the three props (location, history, match) from React Router's Route are not passed to it's component. 似乎来自React Router的Route的三个道具(位置,历史,匹配)不会传递给它的组件。 Am I doing anything wrong with Typescript or with React Router? 我是使用Typescript还是使用React Router做错了什么?

index.tsx 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(); registerServiceWorker();

example component GotNoHistory.tsx 示例组件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;

Update 更新

I've discovered RouteComponentProps . 我发现了RouteComponentProps I added this props to my class GotNoHistory and the error was gone. 我将这个道具添加到我的类GotNoHistory中 ,错误消失了。 Is that the correct way to solve the problem? 这是解决问题的正确方法吗?

import {RouteComponentProps} from 'react-router';
class GotNoHistory extends React.Component<RouteComponentProps<any>, any> {
    render() {
        return <div>Go back {this.props.history.push("/")}</div>;
    }
}

Update for Chris solution 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("/");
    }
}

Results in nearly the same error: 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>}>' TS2339: Property 'history' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{Props & Route<RouteProps>}>'

Solved by myself. 由我自己解决。

My solution was to add the route props to the types of my props. 我的解决方案是将路径道具添加到我的道具类型中。

import {RouteComponentProps} from 'react-router';
class GotNoHistory extends React.Component<RouteComponentProps<any>> {
    render() {
        return <div>Go back {this.props.history.push("/")}</div>;
    }
}

For those who don't know, like me before, what this means <RouteComponentProps<any>> : 对于那些不知道的人,像我之前一样,这意味着什么<RouteComponentProps<any>>

The parameter ist the prop type. 参数是prop类型。 So before it was only React.Component which means that we don't expect any props from my router. 所以在它之前只有React.Component ,这意味着我们不希望我的路由器有任何道具。 So this couldn't work out! 所以这无法解决!

You should have access to it within your route components, but you need to let Typescript know you expect them as props: 您应该在路径组件中访问它,但是您需要让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("/");
  }
}

The line IProps & Route is saying the expected props is a combination of my own props (IProps, whatever you define that as, possibly nothing) and the Route props. IProps & Route这条线说的是预期的道具是我自己的道具(IProps,无论你定义为什么,可能没什么)和道具道具的组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM