使用RouteComponentProps和自定义道具来响应路由

时间:2018-07-25 15:57:43

标签: reactjs typescript react-router

对我遇到的这个问题感到疯狂。

首先,我还是React的新手,其次,在发布之前,我尝试过: Passing custom props to router component in react-router v4react Child component not receiving props

但是没有运气。

我想要实现的只是将路由添加到现有的React项目中。

例如顶级(父亲)类如下:

import * as React from "react";
import { RouteProps } from "react-router";
import { IAppSettings, IAppConfig, IDataAccess } from "../interfaces";
import { AppConfig } from "../appconfig";
import { DataAccess } from "../DataAccess";
import { BookingSiteOverview } from "./BookingSiteOverview";

import { Layout } from "./Layout";
import { NavigationMenu } from "./NavigationMenu";
import { BrowserRouter as Router, Route, Link, Switch, Redirect } from "react-router-dom";
import { RouteComponentProps } from "react-router-dom";

    export class CJV extends React.Component<RouteComponentProps<{}>, {}> {
        private appConfig: IAppConfig;
        private dataAccess: IDataAccess;

        constructor() {
            super();

        const config: IAppSettings = require("Config");

        AppConfig.apiUrl = config.ApiUrl;

        this.appConfig = new AppConfig();
        this.dataAccess = new DataAccess(this.appConfig);
        this.dataAccess.getProfiles = this.dataAccess.getProfiles.bind(this);
    }

    public render() {
        return <div>
            <Layout>
                <NavigationMenu />

                <Switch>
                    <Redirect exact={true} from="/" to="/BookingSiteOverview" />
                    <Route path="/BookingSiteOverview" exact render={(props) =>
                        (<BookingSiteOverview getProfiles={this.dataAccess.getProfiles} {...props} />)} />
                </Switch>


 </Layout>;
        </div>;
    }

注意:错误位于getProfiles={this.dataAccess.getProfiles} 属性'getProfiles'在类型'Readonly <{children ?: ReactNode;上不存在。 }>和“只读”>。

儿童班:

 import * as React from "react";
import { ProgressSpinner } from "primereact/components/progressspinner/ProgressSpinner";
import { RouteComponentProps } from "react-router-dom";

import { IBookingSiteOverviewState, IBookingSiteOverviewProps } from "../interfaces";
import { ProfileSelection } from "./ProfileSelection";
import { DateRangeSelection } from "./DateRangeSelection";
import { CollectionStatsTable } from "./CollectionStatsTable";
import { IBookingSiteCollectionStat, ICollectionStatHiddenColumnDefinition } from "../models";

export class BookingSiteOverview extends React.Component<RouteComponentProps<{}>, IBookingSiteOverviewState> {
    private stats?: IBookingSiteCollectionStat[];

    constructor(props: IBookingSiteOverviewProps) {
        super();

    this.state = { statsLoading: false, applyButtonPressed: false };
}

public render() {
    const statsTable = this.state.statsLoading
        ? <ProgressSpinner />
        : <CollectionStatsTable
            id="booking-site-stats"
          getRowDefinition={this.props.getProfiles} 
//<---- Displays error:     Property 'getProfiles' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<{}>>'.

            stats={this.stats}
            statKey="bookingSiteId"
            rowExpansionTemplate={this.props.getProfiles}
            tableWidth="1341px" />;

    const statsSection = this.state.applyButtonPressed ? statsTable : "";

    return <div>
        {statsTable}
    </div>;
}

}

所以我无法弄清楚如何传递自定义道具,并对路由道具做出反应,以便路由可行,子类将接收其属性。

1 个答案:

答案 0 :(得分:3)

Typescript中的React组件是这样定义的:

class ComponentName extends React.Component<PropsType, StateType> { ... }

在这种情况下,BookingSiteOverview接受RouteComponentProps类型的道具<{}>,并使用IBookingSiteOverviewState类型的状态。

RouteComponentProps <{}>不包含getProfiles的定义,因此会导致错误。

编辑

getProfiles是在IBookingSiteOverviewProps中定义的(根据OP),因此将BookingSiteOverview的道具更改为RouteComponentProps<{}> & IBookingSiteOverviewProps可解决此问题:

export class BookingSiteOverview extends React.Component<RouteComponentProps<{}> & IBookingSiteOverviewProps, IBookingSiteOverviewState> {
    constructor(props: RouteComponentProps<{}> & IBookingSiteOverviewProps) {
        super(props);
        ...
    }
    ...
}