简体   繁体   中英

react routing using RouteComponentProps and custom props

Getting crazy about this issue I have..

Firstly, I am still new to React, secondly, before posting, I tried: Passing custom props to router component in react-router v4 and react Child component not receiving props

But no luck..

what I am trying to achieve, is simply add routing to existing React project.

For example top (father) class looks like this:

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>;
    }

NOTE: error is at getProfiles={this.dataAccess.getProfiles} Property 'getProfiles' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly>'.

Child class:

 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>;
}

}

So I cannot figure out, how to pass custom props, and react routing props, so routing would work, and child class receives its properties..

A React component in Typescript is defined like this:

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

In this case BookingSiteOverview accepts props of type RouteComponentProps<{}> and uses a state of type IBookingSiteOverviewState.

RouteComponentProps<{}> does not include a definition for getProfiles, so that is causing the errors.

EDIT

getProfiles is defined in IBookingSiteOverviewProps (per the OP) so changing the props for BookingSiteOverview to RouteComponentProps<{}> & IBookingSiteOverviewProps resolves the issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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