使用 RouteComponentProps 和自定义propsreact路由

IT技术 reactjs typescript react-router
2021-05-09 10:24:11

对这个问题感到疯狂我有..

首先,我还是 React 的新手,其次,在发布之前,我尝试过:将 自定义props传递给 react-router v4 中的路由器组件, 并且 react子组件未接收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>;
}

}

所以我无法弄清楚,如何传递自定义props,并对路由props做出react,因此路由会起作用,并且子类会接收其属性..

1个回答

Typescript 中的 React 组件定义如下:

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

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

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

编辑

getProfiles 在 IBookingSiteOverviewProps(根据 OP)中定义,因此更改 BookingSiteOverview 的props以RouteComponentProps<{}> & IBookingSiteOverviewProps解决问题:

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