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..
With the react-router v5, we can create routes by wrapping with a component, so that we can easily pass props to the desired component like this. Similarly, you can use the children prop in v5. If you are using react-router v4, you can pass it using the render prop.
The exact prop is used to define if there is an exactly the requested path.
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);
...
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With